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
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 You will get better with practice.
Offline
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.
Offline
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
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
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.
Offline
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
the smiles in this forum don't have noses (click the smilies link), so : ) will turn into but :-) won't.
Paulmedwal
Offline
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)
Offline
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
Offline
For PAULMEDWAL and ARCHMAGE,
Thanks to you both!!
Paul, those are both great tips! Much appreciated!
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.
Offline
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
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
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)
Offline
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.
Offline
I am also making an age of empires-like game using stacks of variables. I'll tell you when I have finished it.
Offline
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
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>
Offline
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.
Offline
Check my scratch cat adventure games. download em and love em. they give the best way too use variables.
Offline
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>
Offline