We like to use Scratch to help teach the basics in coding through interactive stories, games, and animations. If you’ve attended one of our courses, you’ve probably used it before. We’re going to take a moment to talk about variables and why using them will help improve your work in Scratch and any programming languages.
Using Variables Makes Things Simple
Variables can be just about anything, but in Scratch we typically use variables for numbers or text. Variables can do things like:
- Allow you to use the same number in many places but only create or change it once
- Change a number as many times as you want while your program is running
- Track something that is moving on the screen
- Say something you want when you need it
- And more
An Example Using Variables
Imagine your game starting at level 1. Every time you score 5 points we want the game to “level up.” We could take the long way around and do math like 25 / 5 = 5, but that’s a lot of code to think of every possible number we would want to divide by. Variables let us set up things we want to use to make this feature of our game much easier to manage.
Open up your Scratch game and click the Data section. Click Make a Variable. Name it Score. Now repeat and make one named Level.
Step 2: Set up the game
Our game is going to be simple just to show the power of variables so we’re not going to worry about making our sprite (who should be a cat) move. We’re going to make the score go up 1 every second, and at 5 seconds we’re going to level up.
- Go to Events and drag when flag clicked to our workspace on the right
- Go to Data and drag set level to 0 to the workspace directly below the flag
- Go to Data and drag set level to 0 to the workspace directly below your last block
- Change one to Score
- Change the Level to 1
Step 3: Changing our first variable
- Go to Control and drag the forever block below your last block
- In Control, drag wait 1 secs to inside the new forever block
- Go to Data and drag change Level by 1 to after the new wait block
- We don’t want the Level to go up yet; change Level to Score
Hit your Green Flag or GO button to watch your score go up every second. Congrats! You’re counting time!
Step 4: Level up!
- Go to Control and drag the If…then… block after your change Score by 1 block
- Go to Operators and drag the equals operator into the If…then… block
- Go to Data and drag the Score variable (looks like a circle) to the first empty box in your new equals operator
- In the remaining empty box in your equals operator, type 5 (Bonus: if you make this a variable of it’s own!)
- Go to Data and drag change Level by 1 inside your If…then… block
Last step: Reset the score
What are we missing here? We are only leveling up if we reach a Score of 5. We need to set the Score back to 0 to start the count over.
- Go to Data and drag set Level to 0 to after your change Level by 1 block
- Change your new set Level block to set Score to 0
Hit your Green Flag or GO button to watch your score go up every second and your level adjust every time your score reaches 5. Congrats! You now have a way to use variables to track numbers, or data, and have an interaction happen.
Double Bonus: This program will run forever if you let it. How would we cap this at 10 levels?
Bonus Knowledge About Variables
Variables make a developer’s life much easier when doing complex functions and actions. It’s best practice to use variables wherever possible. This way your program becomes more flexible if you want to make little tweaks here and there. Where else could you use variables in this example we provided? Please feel free to share your projects in the comments below.
Use our Variables Example project to get started.