Home > Articles > Scratch resources > 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.

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

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

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About