This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-08-16 23:48:53

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

How can I learn about Variables?

Never having programmed, I'm puzzled by the use of variables.  "Score" as a variable is clear and I've used that several times in projects.    But when I look at the use of variables in other peoples' projects the variables are often puzzling.   The Wikipedia entry on variables in computer science was too brief and too general to be of help.  Is there a helpful book, or are there projects here at Scratch that would introduce variables to a real beginner?  I need a basic idea of how they function in scripting a project.   Thanks!!

Offline

 

#2 2007-08-17 00:14:08

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

Re: How can I learn about Variables?

Really you don't need a book to learn variables. Variables are simply just numbers that you can control and use to control your program. Variables are most useful for if statements and you can check the variables values to determine what script to execute. for example

If score > 1000
say "you got over 1000 points"

if score > 100
say "you got over 100 points''

if score = 0
say "You are really bad at this game"

Variables are not really confusing, what can be confusing is the ways that they are put to use. And don't worry when I started programming variables and if statements confused the hell outta me too  smile  You will get better with practice.


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

 

#3 2007-08-18 14:51:32

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How can I learn about Variables?

The description of the use of "score" is clear.  Could someone describe one or two other variables that would be equally helpful in increasing the power of a Scratch project?

Offline

 

#4 2007-08-18 15:04:45

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

Re: How can I learn about Variables?

A variable can also be used to determine the state of a sprite. For example, if a sprite is walking could be equal to 1, if running equal to 2 and if it's standing equal to 3. Just an example.


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

Offline

 

#5 2007-08-18 18:06:24

pedroskivich
Scratcher
Registered: 2007-05-21
Posts: 72

Re: How can I learn about Variables?

You can use it for a game like for ammo if you shoot whenever you shoot take away one and if it gets to zero you can't shoot.

Offline

 

#6 2007-08-18 18:48:09

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How can I learn about Variables?

So it sounds as if variables help one deal with QUANTITIES.  If no ammo, you can't shoot.  If someone is walking, then it will take a long time to get somewhere.  If someone scores 5 or more points they win the game. 

Or, if there are no seconds left on the clock, then the game is over.

Or, with a variable "PeopleNumber" if there are 3 or more people, then you get CROWD behavior!  If PeopleNumber = 2, then nobody can keep a secret.

Offline

 

#7 2007-08-18 20:27:59

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

Re: How can I learn about Variables?

Yes all that is true, and they can also be used for true and false conditions too.

Usually the number 0 means false  and 1 means true.
here's an example( the variable is called grounded and "ground" is a sprite)

If touching "ground" grounded = 1
else
grounded = 0

when key up arrow pressed
if grounded= 1
broadcast jump

This makes sure he can only jump if he is on the ground.


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

 

#8 2007-08-19 22:58:13

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How can I learn about Variables?

Archmage, how would you construct the phrase "if touching 'ground' grounded = 1" using Scratch blocks? 

"If" requires an insert with pointed ends.  The "=" relationship is built within an insert with ROUNDED ends.  I can build "grounded = 1" but I can't combine it with the "if" block.
IF TOUCHING |sprite4|         (....assuming Sprite4 is the ground)
.........
<(grounded) = (1)>
but I can't combine those two blocks to express THEN.

(I tried to use the "blocks" at the bottom of this form but didn't succeed. When I previewed my result it wasn't what I intended, so I'll have to learn the syntax of using the blocks in these messages.  I kinda got the blocks in there, but didn't know the syntax for filling the blank spaces with names of variables, names of sprites, and numbers....  Oh well, just another challenge....)   :-) 

I hope my verbal try made sense....

BTW, I was able to build your second set of commands.
    IF <(grounded) = (1)>
        BROADCAST |jump|

Thanks!  Larry

    P.S.  it says Smilies are on, but my typed smilies never turn into graphical ones....  yet another challenge.   :-)
    PP.S.  Even worse, when I previewed this message the messaging program substituted partial green blocks for my typed )> characters.  I don't know how to retype it to prevent that, so I hope it's comprehensible.   There must be an easier way.  Or maybe I need to find a "pre-Scratch" language!!  :-)

Last edited by Larry828 (2007-08-19 23:03:31)

Offline

 

#9 2007-08-19 23:03:35

paulmedwal
Scratcher
Registered: 2007-03-09
Posts: 100+

Re: How can I learn about Variables?

the smiles in this forum don't have noses (click the smilies link), so : ) will turn into  smile  but :-) won't.

Paulmedwal


clutter.scratch.mit.edu Visit the Clutter site to create multi-scene stories and multi-level games with Scratch.

Offline

 

#10 2007-08-19 23:05:34

paulmedwal
Scratcher
Registered: 2007-03-09
Posts: 100+

Re: How can I learn about Variables?

btw, just put a space between the parenthesis and the bracket to make it not turn into a Scratch block, example: type ) >

Paulmedwal

Last edited by paulmedwal (2007-08-19 23:06:49)


clutter.scratch.mit.edu Visit the Clutter site to create multi-scene stories and multi-level games with Scratch.

Offline

 

#11 2007-08-19 23:30:37

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

Re: How can I learn about Variables?

Sorry Larry828, my wording of the code is pretty bad it should be like

<when green flag clicked>
<forever>
<if><touching[ "ground"]
<set{ grounded }to( 1)
<else>
<set{ grounded }to( 0)

and also for fun a simple jumping code

<when[ up arrow ]key pressed>
<if><(  grounded<=>1  )>
<repeat(10
<change y by( 4
<repeat(10
<change y by( -4


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

 

#12 2007-08-19 23:46:26

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How can I learn about Variables?

For PAULMEDWAL and ARCHMAGE,

Thanks to you both!! 

Paul, those are both great tips!  Much appreciated!   smile

Archmage, thanks for the coding.  The "set" command is crucial, isn't it?!  So is putting the set command within the Forever block.  I captured your message and printed it out for future reference.   -- And the easy "jump" command is neat too.

Thanks to the contributors to this topic I can feel new neurons by the hundreds springing up in my old brain.   smile

Offline

 

#13 2007-08-20 16:28:04

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

Re: How can I learn about Variables?

For resaonable running speed, replace those if statements with waits to eliminate the busy loop:

[blocks]
<when green flag clicked>
<set{ grounded  }to(  0
<forever>
     <wait until><touching[ ground
     <set{ grounded  }to(  1
      <wait until><<  <not> <touching[ ground  >>
     <set{ grounded  }to(  0
<end>
[/blocks]

Last edited by kevin_karplus (2007-08-20 16:29:06)

Offline

 

#14 2007-08-20 18:25:40

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How can I learn about Variables?

Kevin, I built Archmage's scripts (plus some extras to make a small project) and late last night was watching them run.  With great satisfaction, since the variable was functioning as expected.

But I did notice the endless forever loop running constantly and wondered if there was anything I could do to revise it.  Your suggestion is right to the point.

I'm planning some experiments to see whether Scratch will hold a variable in memory once it is set, or whether it has to be constantly set one way or another.  Your suggestion offers a middle ground: invoke the variable only when a change occurs.

I've had the feeling that using variables could be very powerful (and saw variables used in some of the most impressive projects) but I got stuck trying to find a step-by-step approach to learning how they work.  It would be a good subject for a tutorial for newcomers to Scratch and programming...   Thanks!

Offline

 

#15 2007-08-20 18:32:09

Sawman3
Scratcher
Registered: 2007-07-19
Posts: 97

Re: How can I learn about Variables?

You can't really reverse a forever loop, Larry828, but you can use the following options instead:<repeat(
or
<forever if>
or
<repeat until>

Used along with broadcasts and sets of 'true' and 'untrue' (0 and 1) variable configurations, these can be even more effective than the forever loop.  Hope that helps!  If you want more extensive advice, you can email me at [ADDRESS DELETED].  Good Luck!

Last edited by kevin_karplus (2008-04-09 09:35:17)


"These are the old days, the glory days...they're back."

Offline

 

#16 2007-08-20 18:38:29

JoelP
Scratcher
Registered: 2007-07-20
Posts: 100+

Re: How can I learn about Variables?

Variables are awesome stuff, and the more varied you create them, the more stuff you can do. I am using distance, angles, 3D x, 3Dy and a save copy of every variable so that You have a 3D game that can save, it is a bit too much typing to say how to do it, but gather these up so you maybe can try out how to work it.


do you want to see  real 3D ? come here and have a 3D runaround. the game will soon be a fighting game, as you can see when you press space   http://scratch.mit.edu/projects/JoelP/33274

Offline

 

#17 2007-08-20 18:40:22

JoelP
Scratcher
Registered: 2007-07-20
Posts: 100+

Re: How can I learn about Variables?

I am also making an age of empires-like game using stacks of variables. I'll tell you when I have finished it.


do you want to see  real 3D ? come here and have a 3D runaround. the game will soon be a fighting game, as you can see when you press space   http://scratch.mit.edu/projects/JoelP/33274

Offline

 

#18 2007-11-29 16:05:13

MattO2000
Scratcher
Registered: 2007-11-29
Posts: 6

Re: How can I learn about Variables?

This was SO helpful.
THANKS!!!!!!!!!!
:-)

Offline

 

#19 2007-12-04 20:19:33

manveer
Scratcher
Registered: 2007-12-04
Posts: 2

Re: How can I learn about Variables?

Sometimes when i have a new sprite, the variables list won't show up, how do  make it show up?

Offline

 

#20 2007-12-05 08:54:06

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

Re: How can I learn about Variables?

There are 2 sorts of variables in scratch:  ones that belong only to a specific sprite, and ones that are shared by all sprites.  When you create a new sprite, it will have access to the variables shared by all sprites, but will not have any variables of its own until you create them.

If you duplicate a sprite, then the copy will have its own variables and scripts, but changing these will not change the corresponding variables or scripts of the original sprite.

Offline

 

#21 2007-12-06 12:57:51

MyRedNeptune
Community Moderator
Registered: 2007-05-07
Posts: 1000+

Re: How can I learn about Variables?

You can also use variables to change the speed of your sprite. For example:

<when green flag clicked>
   <set{ speed }to( 1
   <forever>
   <move( <{ speed }> )steps>
      <if><touching[ motorcycle
      <change{ speed }by( 1
      <end>
   <end>


http://i52.tinypic.com/5es7t0.png I know what you're thinking! "Neptune! Get rid of those filthy advertisements and give us back the Zarathustra siggy, you horrible person!" Well, don't worry about it, the Zara siggy will be back soon, new and improved! ^^ Meanwhile, just do what the sig tells you to. >.>

Offline

 

#22 2007-12-09 05:16:01

pasta3049
Scratcher
Registered: 2007-10-18
Posts: 31

Re: How can I learn about Variables?

you also use variables to set the y velocity for gravity

Offline

 

#23 2008-02-08 14:01:25

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: How can I learn about Variables?

A powerful use of variables is to allow one block of code to do one thing.

EG, if you had four "button" sprites that you clicked to make a "Robot" sprite move.

Each button could have
>When clicked, set DIRECTION to xx
>Broadcast "move"

DIRECTION would be 0, 90, 180 or -90 depending on which button.

Then the robot can have:

>When I recieve "move"
>Point in direction DIRECTION
>Move 20

***

Its also very useful to use variables for things that all share the same value but that you might want to change.

Like if you have 10 flashing light sprites, that each have a "Wait 0.5" in them to control the flashing.  You could change the wait to a variable FLASHWAIT and then you can easily change the speed of flashing by changing one entry (That in the Set FLASHWAIT to XX) script, instead of having to go and change 10 different scripts.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#24 2008-02-08 19:37:15

Heybrian
Scratcher
Registered: 2007-12-05
Posts: 100+

Re: How can I learn about Variables?

Check my scratch cat adventure games. download em and love em. they give the best way too use variables.


Black Mesa. go here to get a game 10 times better than SN or FW.  http://scratch.mit.edu/galleries/view/10650
Smiley! copy and paste these into your post or signature!  smile   sad    yikes                 big_smile     wink    tongue    hmm    neutral   cool   lol   mad   roll

Offline

 

#25 2008-03-08 19:00:45

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: How can I learn about Variables?

variables can also be used to control a sprite, just make the variable invisible and you could do some thing like:
[blocks]
<when green flag clicked>
<forever>
<move( <{ X velocity }> )steps>
<end>

<when green flag clicked>
<forever if><key[ right arrow ]pressed?>
<change{ X velocity }by( 1 >
<end>


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

Board footer