This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.
  • Index
  •  » FAQ
  •  » Question on variables

#1 2007-09-17 21:02:16

LD1302
Scratcher
Registered: 2007-09-17
Posts: 1

Question on variables

Could someone please help me with variables?  I want to know the basics of how they work and some applications.  I appreciate the help!

Offline

 

#2 2007-09-17 21:37:14

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Question on variables

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

 

#3 2007-09-17 22:15:19

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Question on variables

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.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#4 2007-09-27 19:10:16

Catmamma
Scratcher
Registered: 2007-09-24
Posts: 8

Re: Question on variables

I need help with them to.
Do you know to use them?


A small girl in a big big world.
In the name of the Moon i will punish you!
smile   smile   tongue

Offline

 

#5 2007-09-28 16:29:00

LeBurt
Scratcher
Registered: 2007-09-12
Posts: 24

Re: Question on variables

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

 

#6 2007-09-28 22:16:26

Catmamma
Scratcher
Registered: 2007-09-24
Posts: 8

Re: Question on variables

Thank you LeBurt.
That is so helpful.
I Didn't know what a variables was so again thank you  smile

Last edited by Catmamma (2007-09-28 22:16:56)


A small girl in a big big world.
In the name of the Moon i will punish you!
smile   smile   tongue

Offline

 

#7 2007-12-12 07:33:35

thething12321
Scratcher
Registered: 2007-12-12
Posts: 6

Re: Question on variables

how do you make enemies just keep respawning out of screen?

Offline

 

#8 2007-12-13 21:05:48

andresmh
Scratch Team at MIT
Registered: 2007-03-05
Posts: 1000+

Re: Question on variables

Great question and answers! I am moving this to the FAQ section.

thething12321: try asking that question on another thread please  smile


Andres Monroy-Hernandez | Scratch Team at the MIT Media Lab
on identi.ca and  twitter

Offline

 

#9 2008-01-16 13:18:57

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: Question on variables

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

 

#10 2008-02-25 15:20:01

certvista
Scratcher
Registered: 2008-02-25
Posts: 1

Re: Question on variables

Really nice information LeBurt . I got many other information as well for my assignment.

Thanks
Mark

Offline

 

#11 2008-03-08 09:53:07

Spencer3e
Scratcher
Registered: 2008-03-08
Posts: 3

Re: Question on variables

<direction><y position><x position> All where they start out

Offline

 

#12 2008-03-08 09:56:35

Spencer3e
Scratcher
Registered: 2008-03-08
Posts: 3

Re: Question on variables

I want them to start out at the bottom ( some of them) but they start out up top, all of them. And my projects arn't very good.

Offline

 

#13 2008-03-08 09:58:10

Spencer3e
Scratcher
Registered: 2008-03-08
Posts: 3

Re: Question on variables

How do you make sprites go flying everywhere.

Offline

 

#14 2008-03-08 10:33:24

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Question on variables

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.


http://i.imgur.com/WBkM2QQ.png

Offline

 

#15 2008-05-31 04:49:57

oli_95
Scratcher
Registered: 2008-01-03
Posts: 3

Re: Question on variables

how do i make gravity to this gamehttp://scratch.mit.edu/projects/oli_95/176708

Offline

 

#16 2008-05-31 19:43:30

CFCRubiks
Scratcher
Registered: 2008-05-11
Posts: 100+

Re: Question on variables

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

 

#17 2008-05-31 21:10:04

Cyclone103
Scratcher
Registered: 2008-03-20
Posts: 500+

Re: Question on variables

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)


All your base are belong to us

Offline

 

#18 2008-06-16 12:10:07

inmotion
Scratcher
Registered: 2007-09-14
Posts: 13

Re: Question on variables

for the next version of scratch could you make it so that you could show a variable and hide a variable during gameplay? it would be very helpful

Offline

 

#19 2008-06-16 12:36:21

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Question on variables

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


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 

#20 2008-07-15 08:36:06

Fireicedude
Scratcher
Registered: 2007-10-24
Posts: 43

Re: Question on variables

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

 

#21 2008-07-30 04:55:57

Aarone32
Scratcher
Registered: 2008-07-30
Posts: 2

Re: Question on variables

Is there a site where I can find some php programming tutorials?...I guess there are a lot more useful information about variable to be found there too.
Thanks
please post here if someone finds such a resource

Offline

 

#22 2008-08-17 02:03:02

BlueFlame
Scratcher
Registered: 2008-08-17
Posts: 100+

Re: Question on variables

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.


Scape - The mindbending puzzle by coolstuff  big_smile

Offline

 

#23 2008-08-17 02:09:41

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Question on variables

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.


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 

#24 2008-08-17 21:35:08

BlueFlame
Scratcher
Registered: 2008-08-17
Posts: 100+

Re: Question on variables

Thanks for the info bluestribute, but I still don't get it. Could you please tell me the blocks I should use for saving and loading a game?

Thanks heaps!

BlueFlame


Scape - The mindbending puzzle by coolstuff  big_smile

Offline

 

#25 2008-08-18 00:02:28

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Question on variables

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


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 
  • Index
  •  » FAQ
  •  » Question on variables

Board footer