How to Use Scratch Variables on the Raspberry Pi - dummies

How to Use Scratch Variables on the Raspberry Pi

By Richard Wentk

When experimenting in Scratch on your Raspberry Pi, maybe you want to make the sprite do something like jump when you press one key, hide behind another sprite when you press a different key, or bounce before it hits the edge.

To do that, you need to know where it is. And you have to be able to change where it is. You can do this in Scratch using variable blocks.

A variable is like a box that holds a number. The box has a name, so you can tell it from other boxes. And it has space for a number.

Variables can remember letters, words, and sentences, too.

Using variables in Scratch

Scratch can do three clever things with variables. The first is make them. Variables have special blocks, and when you make one variable, you get some special blocks to help you use it. You can set the variable to a number or add a number to it.

When you make a variable, it appears on the stage. You don’t always want this, so you can use a hide variable block to make it go away. And you can use a show variable block to make it come back.

The second clever thing is math. You can add, subtract, multiply, and divide variables by some number. You can even add, subtract, multiply, and divide one variable by another!

The final clever thing is best of all. You can use a variable wherever you see a number. For example, you can tell a go to block to use a variable you make. When you click the go to block or when Scratch reaches it in a script, the block moves the sprite to the number stored in the variable.

This gives you way more options than moving a sprite to the same place all the time. You can change the numbers by hand. Or with math. Or by making them follow other numbers, like the position of some other sprite.

Making a variable in Scratch

To make a variable, click the darker orange Variables button at the bottom right of the block types in the block list area. Three buttons appear. You can click them to

  • Make a variable

  • Delete a variable

  • Make a list

A list is a special kind of a variable that holds other variables. It’s like a big box with lots of smaller boxes inside it. They’re numbered so that you can tell them apart and do things like get the third box and change what’s inside it. You can ignore lists for now.

To make a new variable

  1. Click the make a variable pattern.

    You see a window like the one in the following figure.


  2. Type sprite1_x into the Variable Name? box.

  3. Leave the For All Sprites option checked and click OK.

    Whoa! Stuff happens! Scratch makes some new blocks. And if you look at the stage, you’ll see a box appears, with the name of your variable sprite1_x and a number.

When you make a new variable, the number is always 0 because you haven’t changed it yet. The following figure shows what you get.


Can you use variables to replace any number? You totally can! You can use a set block to set a variable to the value of another variable. In a change block, you can make the by value a variable. You can make scripts that are really smart, with variables passing values to other variables between sprites all over the stage. There are almost no limits to what you can do.