Spotlight: The variable.
Make a Maths game for your child using Scratch: The variable.
If you do not already have Scratch or need extra help, use the Support page
The variable holds a value.
You can find the variable in the Code block section of your window (see figure 1)
Note: the online version the variables are called data blocks
Note this is different to the other windows. You have to make the variable by selecting the ‘Make variable’ button. Once you have done that, a window will appear and ask you to name it and select if it is for this sprite or all sprites.
If you select ‘for this sprite only’ the variable will only work for this sprite. If you select the ‘For all sprites’, the variable will work for all the spites. You will find this useful, but for now selecting for all sprites will be fine.
Usually, it is wise to name a variable using something memorable but for this excise just call it test. Once you have named and selected the type of variable, you will see this (figure 2).
You now have five blocks. One is the variable, two manipulate the variable and two show or hide the variable value on the stage.
The rounded orange block holds the value. Note that it has a check box next to it now look at the stage and notice the test variable on the stage. Uncheck the box and the variable disappears. Check the box to make it reappear.
The Set to block determines the value. Drag the set to block into the scripting panel set it to 10 and double click on the set to block. The value in the orange box on the stage changes to 20.
The change by block will change the value in the variable by the value you enter. Drag this block onto the scripting panel, set it to change by 10 and keep it separate from the Set to block. Double click on it and see what happens on the stage. The value has changed by ten. Each time this block is used it will change the value of test by ten. Try it.
The Show and Hide variables will show and hide the variable on the stage. Try it.
The variable holds a value. It can hold integers like 1, 2, 56, or 1.1, 5.56. It does have a limited though. Try setting the variable to a really big number and then changing it by a really big number. What happens? Furthermore, the variable will continue to hold the same value.
You can add some of the blocks into the set to and change orange blocks from other code block sections such as from the operator section. Try this. (see figure 3)
The keys points
- You can hold a value in a variable
- You can change the value in the variable
- The value will stay in the variable until otherwise instructed. This is why it is important to set the variables to the original value at the beginning of the program.
Next spotlight: The Operators.