Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Variables Tutorial

This tutorial will explain what variables are, how to make them, what you can do with them, and how to use them in your projects. Variables in Scratch consist of a series of symbols to represent a value. Mathematically they are symbols that represent numbers.

Contents

What are Variables?

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
  • {nothing — empty string}

How do you make Variables?

In the variables palette, click Make a var.png. A box will appear. Type the name of the variable you want to create, and select weather it should be "for all sprites" (global) or "for this sprite only" (private). Press OK.

What do you do 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 I use variables 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 your own uses:

when gf clicked
set [Health v] to (10)
forever
   if <touching [Lava v]?>
      change [Health v] by (-1)
   end
   if <touching [Healer v]?>
      change [Health v] by (1)
   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 must be checked: Checkbox to show or hide variable.png

See Also

  • This page was last modified on 12 April 2014, at 14:05.