This tutorial explains what variables are, how to make them, what one can do with them, and how to use them in one's projects. Variables in Scratch consist of a series of symbols to represent a value; mathematically they are symbols that represent numbers.
What are Variables?
- Main article: Variable
A simple way to think about variables is a box with numbers in it. These numbers can be increased and decreased and made to control various parts of a project. Contrary to algebraic variables which represent unknown quantities, the variables in Scratch and other programming languages have known values. In Scratch 1.3 and above, variables can contain text (strings), numbers, or booleans (true/false values). Some examples of possible values of variables are provided below:
- Hello, world!
- 123
- 3.14
- 0
- -321
- true
- false
- {nothing — empty string}
How are Variables made?
In the variables palette, click , and a box will appear. Type the name of the variable, and select whether it should be "for all sprites" (global) or "for this sprite only" (local). Press OK.
What can be done with Variables?
Variables have theoretically infinite uses. One of the most common is to simply store values — this only requires Set () to ().
Another common use is to create efficient scripts. As a variable's value can change, variables are often used in blocks that contain number or text input.
A common example is velocity: Here is a simple script for a falling sprite in an animation project. It works, but the script is inefficient, and takes more time to code:
when I receive [Fall v] change y by (-1) wait (0.1) seconds change y by (-2) wait (0.1) seconds change y by (-3) wait (0.1) seconds change y by (-4) wait (0.1) seconds change y by (-5) wait (0.1) seconds change y by (-6) wait (0.1) seconds change y by (-7) wait (0.1) seconds change y by (-8) wait (0.1) seconds change y by (-9) wait (0.1) seconds change y by (-10)
The script performs the same action ten times, except that the value changes at a constant speed. A variable could be used there:
when I receive [Fall v] set [speed v] to [0] repeat (10) change [speed v] by (-1) change y by (speed) wait (0.1) seconds end
Both scripts work the same way, but the latter is more efficient, because it uses variables and it shortens the length of the script, which wastes less time and, in some cases, creates less lag.
Variables can also be used to store Cloud Data. This can be useful in multiplayer games, as it allows Scratchers to interact with each other.
How can Variables be used with their Stage Monitors?
Another common use for variables is for displays - as each variable gets its own Stage Monitor, it makes variable displays very easy. Stage Monitors can serve a variety of purposes:
- Displaying health
- Displaying score
- Showing completion of levels (Example: 1/5 Missions completed)
- Showing time
- Displaying speed of movement
- Adjustable Variables (using sliders) for interactive games
Health displays are very simple. They only take a few steps:
- When the project is started (i.e. the Green Flag is clicked), the variable must be set to the maximum amount of health
- When the player gets hurt, the variable must decrease
- When the player gets healed, the variable must increase
Here is a simple script to perform these actions — there are many different types of games with health, so it may have to be changed for one's own uses:
when gf clicked set [Health v] to (10) forever if <touching (Lava v)?> then change [Health v] by (-1) wait until <not<touching (Lava v)?>> end if <touching (Healer v)?> then change [Health v] by (1) wait until <not<touching (Healer v)?>> end
In order for the variable to be displayed on the stage, the box next to the variable being displayed in the variable block palette must be checked:
When a variable is created, it will show on the stage by default.
The Show Variable () and Hide Variable () blocks can also be used to control this within a script.
How can Variables be Deleted?
To delete a variable, right click the variable, and hit the delete button. On a mobile device, hold down on the variable and when the option appears, tap delete or by clicking delete in the variable selection dropdown.
Note: | Variable usage is automatically deleted as of Scratch 3.0, but remember to clean up the blocks around it. |
How can Variables be Renamed?
To rename a variable, right click the variable, and hit the rename button. On a mobile device, hold down on the variable and when the option appears, tap rename or by clicking rename in the variable selection dropdown.