That is a big question. Basically, variables are how your program remembers things (like scores or whether you are currently carry a sword of destruction).
Read the scripts for some fairly simple programs (like the ones that come with scratch) to see how other people have used them.
Offline
As kevin said thats a hard question to answer because variables have infinite applications. In case you don't what a variable is it is a number you can alter and use in your if statements and other code involving numbers.
I recommend you look at the role variables play in some simple projects and use your imagination to see how you can apply variables to your own projects.
Offline
Hi LD1302 and Catmama,
This is a very good question as variables are a central concept to any form of programming. I'm going to take a step back from variables and start at the beginning. No doubt you have heard the term RAM, as in "my computer has 1 gigabyte of RAM"? RAM is memory used to store and retrieve information such as numbers, strings of characters or pixels in an image.
To get to the variable concept, I'll use an analogy.
Let's imagine that your 1GB of RAM is a golf course and that every byte on that field is a blade of grass. You, on your two legs, are the microprocessor and you are currently running a game program. Of course you want to remember the current score for as long as the game is running. You take a pen and write that score on a single blade of grass for safekeeping. Then you turn around and take off to do some other stuff like move a ship sprite.
Suddenly the player hits an enemy vessel and scores 1,000 points. Quick, you take your pen out and rush to find that blade of grass where you wrote the previous score, but wait a minute, where was it again? Suddenly, the whole field looks the same, every blade of grass is similar and you can't find it. You're stuck!
Well, in reality, a computer doesn't get stuck like that because it uses variables. A variable is a little flag with a name on it that is used to remember where you wrote that score. If we continue with the analogy, when you wrote the score for the first time, you would have planted that flag in the grass so it's easy for you to spot it later on when you need it.
To summarize:
- The blade of grass is where you write the value you want to remember, in our example, the current score of the game. This is called the "value" of a variable.
- The flag is where you write yourself a reminder to be able to find that value when you need it. This is called the "name" of the variable, or its "descriptor".
In Scratch:
When you make a variable, you first give it a name such as "Score" or "Enemies Destroyed" or basically anything you want. You could name all your variables using single letters like a, b, c, etc. but that wouldn't help you much to remember what they mean. That's why it is always better to give a meaningful name.
When your variable is created, you can the use it in many ways. Let's suppose you created a variable named Score.
First, you can set it to a specific value using the "Set Score to" block. For example, at the beginning of the game, you would want to set the score to 0, like this:
[blocks]<set{ Score }to( 0[/blocks]
Next, you can change its value by a certain amount using the "Change Score by" block. For example, the player hit an enemy ship worth 1,000 points, then:
[blocks]<change{ Score }by( 1000[/blocks]
Finally, you can use variables to calculates the values of other variables. For example, your could have a powerup in your game that multiplies the points for a hit by 2, 3 or 5. You could have a variable named "Score Multiplier" and another one named "Score". When the game starts, you would set your variables like this:
[blocks]<set{ Score }to( 0
<set{ Score Multiplier }to( 1[/blocks]
When the player captures a powerup, you would increase Score Multiplier by 1:
[blocks]<change{ Score Multiplier }by( 1[/blocks]
When the player hits an enemy, you would have to change score by 1,000 multiplied by the Score Multiplier variable, like this:
[blocks]<change{ Score }by( (( 1000 <*> <{ Score Multiplier }> ))[/blocks]
As you can see, variables are very powerful and useful when programming. They allow the computer to remember stuff and can be modified as your program runs.
Hope this helps. Don't hesitate to ask questions if you have them.
LeBurt.
Offline
how do you make enemies just keep respawning out of screen?
Offline
Great question and answers! I am moving this to the FAQ section.
thething12321: try asking that question on another thread please
Offline
To add to LeBurts excellent explanation:
There are also two kinds of variable that you can make in Scratch. By default, when you make a new variable, it will be "For all Sprites". If desired, you could click "For this Sprite Only". This leads you to two good questions: What's the difference? Why would I want to do that?
Let's carry LeBurts "Golf Course analogy" a bit further: Every sprite in your project has the ability to move around and write numbers on the blades of grass (erasing the number that was there) and plant flags. If I'm a Sprite that needs to remember something that is only important to me, like how many times I've done a task, I don't want other Sprites to come along and replace my number. This is where "For this Sprite Only" (also called "local") variables come in. Think of each sprite as having a little fenced-off area that only they can go in. This is where they write their local variables.
This makes debugging your program easier. If, for instance, you decided to keep track of how many times you bump into another sprite, you could use a global variable. If you bump into anyone, you'd run over to the BumpedInto flag, read the number, erase it and put down the next number. But if the sprite that bumped into you did the same thing, the number would increase by 2. By keeping the variable local to you, this won't happen.
Scratch now has the ability to let sprites "peek" over the "fence" and see what you've written in your local area. (But other sprites can't change the number). This is what the new "Sensing Block" will let you do. Before you fill out the new block, it will say something like ( x position | of | Sprite1). The block has two pull downs, the one on the right lets you select a sprite. One you do that, you are peeking over that sprite's "fence". You will be able to see that sprite's x position, y position, direction, costume#, and below a horizontal divider in the pull-down, you'll see that sprites "local" variables. The new block will let you set one of your variables to the same value as another sprite's, or change your x-position to the same as the other sprite's.
-MrEd
Last edited by EdnaC (2008-01-19 23:54:29)
Offline
Well a variable is a word or something that stands for a number. Like @=7 for example. In scratch you can change the variable and use it with other variables and multiply it, divide it and more.
Offline
I just use variables (sometimes) to replace the most wanted [blocks]<if>[/blocks]I recieve. I set it to a certain number ,and instead of broadcasting, I use the changeing variable script.
Offline
A variable is one labeled number that can have a number of purposes. For example, for a score being enough to win a game <if><( <{ score }> <>> ____ )><broadcast[ win!
Last edited by Cyclone103 (2008-05-31 21:10:32)
Offline
And varaibles can also help save (in a way). I use them a lot in my pokemon game to remember things like health and if you've caught a Pokemon™ Yet
Offline
Bluestribute wrote:
And varaibles can also help save (in a way). I use them a lot in my pokemon game to remember things like health and if you've caught a Pokemon™ Yet
Yeah, they can work as a database type thing too. Or like an on off switch.
Offline
Hi, I just made an account and I was only told about Scratch about a month ago. I've figured out a lot about the program, but I want to ask a question about a comment that Bluestibute made. He said: "And varaibles can also help save (in a way). I use them a lot in my pokemon game to remember things like health and if you've caught a Pokemon™ Yet"
I understand the analogy that LeBurts used, but my question is: How can you use variables to save your game. And then how can you load it again by using variables? If someone could tell me this would be a MASSIVE help. Thanks.
Offline
BlueFlame wrote:
Hi, I just made an account and I was only told about Scratch about a month ago. I've figured out a lot about the program, but I want to ask a question about a comment that Bluestibute made. He said: "And varaibles can also help save (in a way). I use them a lot in my pokemon game to remember things like health and if you've caught a Pokemon™ Yet"
I understand the analogy that LeBurts used, but my question is: How can you use variables to save your game. And then how can you load it again by using variables? If someone could tell me this would be a MASSIVE help. Thanks.
O.K., before I tell you know this. You can't actually save. The closest would be passwords. But if you wanna save AS YOU ARE PLAYING (and never come back to that as a saved point again), than it is very simple:
You create 2 sets of variables for a simple save: 1 set saves the data at the moment, the second set sets the data when loaded. Now you can also set variables as a certain number to remember things like health (I used the X position). I have a simple saving tutorial here that kinda explains this.
Offline
BlueFlame wrote:
Hey everyone,
Just another question. Can you hide and show variables during gameplay? If you can, could someone please tell me how to do it.
THANK YOU SO MUCH!!!
BlueFlame
Using a cover over the variable. Like my Bomb Squad game and Paddle2See's Good Vibrations
Offline