It's Sean!

UK freelance journalist, author
and writer Sean McManus

Printed from www.sean.co.uk. © Sean McManus.

NEW: Sign up for my newsletter!

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.

Books by Sean McManus

Scratch Programming in Easy Steps

Scratch Programming in Easy Steps

Raspberry Pi For Dummies

Raspberry Pi For Dummies

Learn to program with the Scratch programming language, widely used in schools and colleges.

Set up your Pi, master Linux, learn Scratch and Python, and create your own electronics projects.

Coder Academy

Coder Academy

Cool Scratch Projects in Easy Steps

Cool Scratch Projects in Easy Steps

Learn to make games and other programs in Scratch 2.0, and make a web page in HTML, with this highly interactive book for 7-10 year olds.

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

More books

Recommended articles

Stylised coloured photo of a synth

Discover my music and home recording articles

Interested in starting home recording? Follow me as I start recording, using instruments such as the Novation MiniNova and Korg Kaossilator, and share my synth and sample-based music online.

Scratch cat and rainbow circle

10-block demos for Scratch programming

Lots of people of all ages are learning to program with Scratch. Discover some useful tricks and techniques in my 10-block Scratch demos, including special effects you can drop into your games.

Paper Raspberry Pi case

Download free book chapters and more!

Download a free case for your Raspberry Pi, and free chapters from my books about the Raspberry Pi, Scratch, web design and more!

©Sean McManus. www.sean.co.uk.