From Test-Scratch-Wiki

Revision as of 13:12, 13 June 2016 by InterwikiBot (talk | contribs) (Uploading Page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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 whether 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)
   if <touching [Lava v]?>
      change [Health v] by (-1)
   if <touching [Healer v]?>
      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

You can use the Show Variable () and Hide Variable () blocks to control this within a script.

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.