It's Sean!

UK freelance journalist, author
and copywriter Sean McManus

Printed from www.sean.co.uk. © Sean McManus.
Home > Books > Scratch Programming in Easy Steps > Making Scratch variables

What's the difference between a variable made for one sprite and a variable made for all sprites in Scratch?

Sean McManus, author of Scratch Programming in Easy Steps and Cool Scratch Projects in Easy Steps, explains the difference between global and local variables in Scratch

When you make a variable, Scratch asks you whether you want it to be for all sprites or just for one sprite. It's important to get the answer right, because it has a big impact on how your variables work.

In short, a variable for all sprites (a global variable) can be seen, changed and used by any sprite. A variable for one sprite belongs to that sprite alone. Other sprites can see its value, using the [x position of Sprite 1] Sensing block, but they can't change it.

These variables are usually called local variables in other programming languages. Two sprites can have local variables with the same name, but they're different variables, which can have different values.

Here's a demo of local and global variables in Scratch

Code for Sprite 1 (the cat)

Create the variable this year for ALL SPRITES.

when green flag clicked, set this year to 2014, say how old are you?, broadcast ask age

Here's the code for Sprite 2 (the boy in my example)

Create the variables birth year and my age FOR THIS SPRITE ONLY

when i receive ask age, set birth year to 2001, set my age to this year minus birth year, say my age

Here's the code for Sprite 3 (the girl in my example)

Create the variables birth year and my age FOR THIS SPRITE ONLY

when i receive ask age, set birth year to 2002, set my age to this year minus birth year, say my age

The end result

You now have a variable called "this year", which all sprites can use to calculate their age. Each sprite has a variable called "my age" and "birth year". Although these variables have the same name, they're different variables - the boy and girl have their own birth year and ages, and other sprites can't change them.

This becomes really useful when you start duplicating sprites. If you duplicate a sprite that has a local variable, the new sprite will also have a local variable with that name. That stops sprites from interfering with each other's information. What happens if you use a global variable for "my age" or "birth year"? The sprites all start changing the same values, so their ages can get muddled up.

When I made this program, I made the boy sprite and then duplicated it for the girl sprite and then just changed the value of the birth year variable (and the name and picture). Because they were local variables, I was able to do that. If they had been made for all sprites, all the sprites would have the same value in the age variable at the end of the program.

In the picture below, I've shown all the variables for the sprites, together with the sprites. I've also duplicated the girl sprite and made it into Gobo, with its own age and birth year.

Find out more...

Find more 10 block Scratch demos here. For more information on my books and more bonus content, visit the Scratch Programming in Easy Steps and Cool Scratch Projects in Easy Steps homepages.