getting started with scratch

VARIABLES

Variables

A variable is a way of storing numbers and/or text.

You can make your own variables and choose whether they show on the Stage:

Add a variable in Scratch

  • Click on Variables in the Code tab, then click on Make a Variable.

    Variable blocks

  • Type in the name of your variable. You can choose whether you would like your variable to be available to all sprites, or to only this sprite. Press OK.

    Create variable

  • Once you have created the variable, it will be displayed on the Stage, or you can untick the variable in the Scripts tab to hide it.

    Variable on the stage

You should think about whether you need to set a starting value when you make a variable:

Make a variable and set a start value

Click on Variables in the Code tab, then click on Make a Variable.

Variable blocks

Type in the name of your variable. You can choose whether you would like your variable to be available to all sprites, or to only this sprite. Press OK.

Create variable

The variable will show on the Stage:

Variable on the stage

If you want to hide the variable on the Stage, uncheck the box next to the variable in the Variables blocks menu.

Setting a start value

If your variable should have the same starting value every time your project is run, then add a script to set it:

 

 

 

 

 

You can rename a variable if you change your mind:

Rename a variable

Sometimes you think of a better name for a variable.

You can give a variable a new name: go to the Variables blocks menu, right-click (or on a tablet, tap and hold) on the variable, and choose Rename variable.

Selecting 'Rename variable' in the menu.

This will change the name of the variable in all the blocks in which you have used it.

Tip: Make sure you know the difference between set and change by. set will replace the value stored in a variable. change by will change the value of a number variable by the amount you choose, change by1 will add one to a variable. change by-1 will take one away from a variable.

Tip: Variables on the Stage always appear in a layer above all of the sprites. If you have a moving sprite, the sprite will go under any variables on the Stage.

You can use a slider to control the value in a variable.

Use a slider to set a variable

Right-click on your variable on the Stage.

Select slider in the menu.

Selecting 'slider' in the menu.

Tip: On a tablet, double tap on the variable on the Stage to change between the display options.

Right-click again on the variable displayed on the Stage and select change slider range.

Selecting 'change slider range' in the menu.

To set the range, specify a minimum and maximum value.

For example, a minimum of 1 and a maximum of 10 work fairly well for how quickly a sprite spins.

Changing the slider range to 1 to 10.

The value range that is right for your variable depends on what you are using it for. Sometimes you will need to experiment a little to get it right, but remember that you can change it at any time.

Drag the slider to a convenient position on the Stage.

Tip: If you are using a computer with a keyboard, then you can use the arrow keys to change the value of a slider by 1. First, click on the slider to select it, then use the Left arrow key to change the value by -1 and the Right arrow key to change the value by 1.

More ideas for using variables:

Create a high score in Scratch

It’s fun to keep track of a high score in a game.

Let’s say you have a variable called score, which gets set to zero at the beginning of each game.

Add another variable called high score.

variables menu with Make a Variable highlighted

new variable popup box with high score as the variable name

At the end of the game (or whenever you want to update the high score), you’ll need to check whether you have a new high score.

 

 

 

Join text and variables in Scratch

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Set a variable with a button

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Change a variable in a loop

 

Control graphic effects with a variable slider

Here are some examples of how you can use your variable slider:

This example changes the ghost graphic effect so you can see through the rainbow.

Transparent rainbow: See inside

You can use a transparent variable to control the ghost effect on a sprite at the front, and use it to reveal or hide sprites in lower layers.

 

 

 

 

 

 

 

 

 

Try using the same approach with other graphic effects such as brightness or color.

Control movement speed with a variable

Ladybug on a wall: See inside

Use the speed variable as the input to a move block:

 

 

 

 

 

 

 

Set the minimum and maximum values for the range of the slider to suit your project.

Control turn speed with a variable

Space dog: See inside

Use an angle variable as the input to a turn right block:

 

 

 

 

 

 

Change the delay in a wait block

Skeleton dance: See inside

Use a delay variable as the input to a wait block:

 

 

 

 

 

 

 

A delay of more than one second is quite a long time. If you include a decimal number in the range of a slider, then you can change the value by a 100th of a second at a time.

A range of 0.00 to 1.00 allows you to choose a delay between 0 seconds (no delay) and 1 second.

Changing the slider range to 0.00 to 1.00.

Change the numbers to get the minimum and maximum values that you want users to be able to use in your project.

Change sound effects

Drum pitch: See inside

You can use a variable in a set pitch effect to block. If you increase the pitch of a sound, the notes get higher and the sound speeds up.

 

 

 

 

 

 

Play the sound in a separate forever loop, so that the pitch will change immediately, rather than waiting until the sound has finished playing:

 

 

 

 

 

 

You can also use a variable to change the volume and pan (tilt) effect.