Chapter 9: Variables (Memory & State)
Store changing values—score, lives, speed, and game state—and master proper initialization and scope.
Lesson goal & core concept
Initialize variables reliably and choose between global ("for all sprites") and local ("for this sprite only") variables.
Think of a variable as a labeled box. "set" replaces what is inside; "change" adds or subtracts from what is already there.
Variable & List Visualizer (1-Indexed Scratch Lists)
Click the Scratch data operations below to watch variables update and see how Scratch 1-indexed list items shift when you add, insert, or delete.
Step-by-step practice
Work through these steps in the Scratch editor, predicting each result before clicking the green flag.
- Create a score variable and reset it to 0 under the green flag.
- Increment score when collecting a star, with a short wait or hide so it counts once.
- Use a "state" variable ("title", "playing", "gameover") to control phases.
- Use local variables when each clone needs its own HP or speed.
- When green flag clicked
- Set score to 0
- Set lives to 3
Try it: Starts every game cleanly with fresh counters.
Mini challenge & next steps
Swap the values of two variables A and B using a third temporary variable "temp".
FAQ
How long should I spend on "Chapter 9: Variables (Memory & State)"?
Aim for 15–20 minutes: read the concept, test the small example, modify one value to see what changes, and try the mini challenge.
This guide is provided for educational purposes. Scratch screens and terms may change over time, so please check the official Scratch website as well.