Learning Goals 3 min
By the end of this lesson you will be able to:
- Make a variable and explain what one actually is.
- Show and hide a variable’s readout on the Stage.
- Say why a loop cannot count without one.
Warm-Up 7 min
New arc, new project — and the last one of Level 1. Start fresh with a Blue Sky backdrop, a cat at the bottom and a piece of bread up above. Everything you have built so far has had one thing in common: the moment it finishes, it forgets everything.
Quick-fire puzzle
Wei Jie wants the cat to count its steps out loud — 1, 2, 3 and so on. He built this and every single line said the same thing. Why?
when flag clicked
repeat (5)
move (30) steps
say [1] for (1) seconds
end
Reveal the answer
The say block contains the word “1”, and a word cannot change. Every pass through the loop says exactly what is typed in the block, because there is nothing in the project capable of holding a number that grows.
He does not need a cleverer loop. He needs somewhere to keep a count — which is what a variable is.
New Concept — a box with a name 15 min
Think of a variable as a small labelled box that your project can put a number in and take a number out of. The label never changes; the contents can change as often as you like.
What a variable actually is
Two parts, and it is worth keeping them separate in your head:
- A name you choose —
score,lives,speed. This is fixed once you make it. - A value the project holds — 0, then 1, then 7. This changes constantly.
When a block says score, it means “whatever number is in that box right now”. That is the whole idea, and it is the thing that separates a project that reacts from a project that remembers.
Making one
Click the dark orange Variables dot in the Blocks Panel. Back in Lesson 3 you noticed this category was nearly empty — this is why. It fills up only once you have made something.
Click Make a Variable, type a name, and press OK. The category immediately gains a set of blocks, all referring to your new variable by name.
Names matter more than you would think
Call it score and every script you write is readable a month later. Call it a and you will have no idea what it was for. This costs nothing at the time and saves a great deal later.
The readout on the Stage
Making a variable also puts a small orange box in the top-left of the Stage showing its current value. The tick box beside the variable in the Blocks Panel turns that on and off.
For a game score you want it visible. For something the player should not see — a hidden timer, a secret count — untick it.
Why it matters
Everything that makes a project feel like a game rather than an animation needs a variable. Scores, lives, levels, timers, speed. Without one, a project can only react to what is happening right now; with one, it can remember what has happened.
Worked Example — a counter that actually counts 15 min
We solve Wei Jie’s problem from the Warm-Up, which is the clearest possible demonstration of what a variable is for.
Step 1 — Look at the empty category
Click the dark orange Variables dot. Almost nothing there — just two buttons. This is the category you noticed was empty back in Lesson 3.
Step 2 — Make one
Click Make a Variable. Type score. Leave “For all sprites” selected and click OK.
Two things happen at once. The category fills with blocks that did not exist a moment ago, and an orange readout appears in the corner of the Stage showing 0.
Step 3 — Find it in the block list
At the top of the Variables category there is now a small rounded block simply labelled score. That rounded shape means it holds a value — it is not something you run, it is something you use inside other blocks.
Step 4 — Drop it into a say block
Drag a say [Hello!] for (2) seconds into the Script Area, then drag the rounded score block directly into its white oval. The oval accepts it and the block now reads “say score”.
when flag clicked
say (score) for (2) seconds
Step 5 — Prove it is reading the box
Click the flag. The cat says 0. Now click directly on the readout on the Stage — you can drag it, but you cannot type into it — so instead use the Variables category and drop in a set [score v] to (0) block with 5 in it, run that, then run the say again.
The cat now says 5. Nothing about the say block changed. It reads the box every time.
Step 6 — Solve Wei Jie’s problem
Now the loop that could not count.
when flag clicked
set [score v] to (0)
repeat (5)
change [score v] by (1)
move (30) steps
say (score) for (1) seconds
end
Step 7 — Watch the readout keep pace
Run it and watch the orange box in the corner as well as the speech bubble. They change together, because they are the same number shown two ways.
Step 8 — Try it without the reset
Delete the set [score v] to (0) and run it twice. The second run counts 6 to 10, because the box still held 5 from last time.
Variables stick between runs, exactly like position, size, visibility, backdrop and volume. This is the sixth kind of state, and the habit is the same one you have built all level.
What changed: your project can hold on to a number between one moment and the next, which is the difference between reacting and remembering.
The full assembled stack (your reference)
when flag clicked
set [score v] to (0)
repeat (5)
change [score v] by (1)
move (30) steps
say (score) for (1) seconds
end
Try It Yourself — three small builds 12 min
Goal: Make a variable called lives, set it to 3, and have the cat announce it.
when flag clicked
set [lives v] to (3)
say (lives) for (2) seconds
Think: two variables can exist at once, each with its own box and its own readout. Making a second one does not disturb the first.
Goal: Count key presses. Every time the space bar is pressed, the score goes up by one and the readout follows.
when [space v] key pressed
change [score v] by (1)
when flag clicked
set [score v] to (0)
Think: two separate scripts. The flag resets; the key counts. That split is exactly how a real game handles its score.
Mini-mission: count down instead. Build a variable that goes down — three lives that reduce by one each time a key is pressed, and a message when they run out.
It works if: the readout starts at three, falls by one per press, and the cat says something when it reaches zero. Pressing more after that should not send it negative. Keep each stack under 8 blocks.
Think: the last criterion is the hard one. With only the blocks you have so far, how could you stop it going below zero — and what would you have to reset to try again?
Mini-Challenge — the step counter 5 min
“How far did the cat walk?”
Build a cat that keeps a running total of how far it has travelled, and announces it at the end.
It works if:
- One flag click runs the whole thing.
- The readout shows the total distance actually travelled, not the number of steps taken.
- The cat announces the final total at the end.
- Running it twice gives the same answer both times.
Reveal one valid solution (teacher)
when flag clicked
set [steps v] to (0)
repeat (4)
move (30) steps
change [steps v] by (30)
end
say (steps) for (2) seconds
The trap is changing the variable by 1 rather than 30 — which counts moves, not distance. Both are reasonable things to count; the criterion says which one is wanted, and pupils have to read it carefully.
Recap 2 min
A variable is a labelled box holding a number. The name is fixed; the value changes. Blocks that mention it read whatever is inside at that moment, which is what lets a loop count and a game keep score. Making one fills the Variables category and puts a readout on the Stage. And like everything else in Level 1, a variable’s value survives between runs — so a game that does not reset its score will start where the last one finished.
- Variable
- A named box holding a value that can change while a project runs.
- Value
- The number currently inside the box.
- Readout
- The orange display on the Stage showing a variable’s current value.
- For all sprites
- A variable every sprite can see and change. The right choice for a score.
Homework 1 min
Two boxes. Make two variables and prove they are completely independent of each other.
- Make variables called
scoreandlives, and show both on the Stage. - Build a key script that changes only one of them.
- Press it several times and confirm the other readout does not move.
- Screenshot the Stage with both readouts visible.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
when [space v] key pressed
change [score v] by (1)
Bring back next class:
- Your screenshot.
- Your answer to: “Why did Wei Jie’s loop say ‘1’ five times, and what exactly does a variable give you that a typed number cannot?”
Heads up for next class: SCR-L01-41 covers set, change and show — the three blocks that do everything a variable needs.