Topic Guide

Variables and State

A variable stores a value that can change. State records which set of rules currently applies.

Why this matters

Scores, timers, fuel, pet moods, and robot modes all have to be remembered. Giving each value a clear name lets a student inspect it while the project runs.

Beyond this topic

A taxi meter keeps the current fare while the vehicle moves. A game score or robot mode also stores information now so that another rule can use it later.

Math and logic

Name

The variable name says what the value represents.

Initial value

The project sets a known value before the test begins.

Update

A particular event changes the value.

State

A label such as PLAYING, CAUGHT, or DELIVERING selects the current rules.

Small examples

1

Score

Add one when an action finishes, rather than on every frame.

2

Pet mood

Store a changing need and let it affect the pet's behaviour.

3

Game state

Use STARTING, PLAYING, WON, and LOST to control separate rules.

4

Robot mode

Record whether the machine is waiting, moving, or recovering.

Program connections

Where this topic appears.

Active now

Scratch & Game Design

Students author games, animations, simulations, and strange interactive stories while learning how events, rules, and state fit together.

Active now

Robotics

Students write commands, wire simple circuits, read sensors, and trace physical cause and effect through the Sense–Decide–Act loop.

Coming later

Roblox Studio / Lua

A possible later step from visual programming into Lua, 3D objects, events, and larger game rules.

Coming later

AI & Smart Machines

A possible later program for testing patterns, predictions, prompts, mistakes, and the limits of AI tools.

Common mistakes

No reset

A new game inherits the score or mode from the previous run.

Several scripts update one value

The score jumps because more than one rule changes it.

Vague names

Variables called thing or number make the code harder to read.

Hiding the display

Removing a variable monitor from the stage does not erase its value.

Put the idea to work

Read the Lesson
Scratch & Game Design Computer Lab Project

Grandma's Intergalactic Taxi

Navigate a space taxi between coordinate-based pickups and destinations.

  • Coordinates
  • Movement
  • Distance
  • Sequences
See how it works
Robotics Guided Project

micro:bit Reaction Timer

Build a reaction timer with a random delay, an early-press rule, and results in the correct units.

  • Input
  • Timing
  • Variables
  • Randomness
See how it works