Revision as of 17:35, 11 March 2018 by Cinnamon bun puff (talk | contribs) (How can variables be renamed?: Fixed image (old was to delete a var.))

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: Variables

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 are usually unknown), the variables in Scratch and other programming languages are simply known values. In fact, in Scratch 1.3 and above, variables can contain text (strings), numbers, or booleans (true/false values). Some examples are below:

  • Hello, world
  • 123
  • 3.14
  • 0
  • -321
  • true/false
  • {nothing — empty string}

How are Variables made?

In the variables palette, click Make a var.png. A box will appear. Type the name of the wanted variable, and select whether it should be "for all sprites" (global) or "for this sprite only" (private). 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 the Set () to () block and is not hard at all.

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:

when I receive [Fall v]
change y by (-1)
wait (0.1) secs
change y by (-2)
wait (0.1) secs
change y by (-3)
wait (0.1) secs
change y by (-4)
wait (0.1) secs
change y by (-5)
wait (0.1) secs
change y by (-6)
wait (0.1) secs
change y by (-7)
wait (0.1) secs
change y by (-8)
wait (0.1) secs
change y by (-9)
wait (0.1) secs
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) secs

Both scripts work the same way, but the latter is more efficient, because it uses variables and it shortens the length of the script.

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 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 should be changed for one's own uses:

when gf clicked
set [Health v] to (10)
   if <touching [Lava v]?> then
      change [Health v] by (-1)
   if <touching [Healer v]?> then
      change [Health v] by (1)

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 must be checked: Checkbox to show or hide variable.png

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 on the variable and hit delete.
Deleting a variable.
Note Note: Remember to remove all blocks that influence the target variable before deleting.

How can variables be renamed?

To rename a variable, right click on the variable and hit rename.
Renaming a variable.

See Also