Variables
Creating memory slots to save user scores
Definition
Variables are labeled memory boxes that store variable values (like scores, coordinates, or timers) that can change during execution. Key concepts include Hat Block, Sensing Block.
Think of Variables as:
Just as an actor changes costume or walks across the theater grid on cue, Variables dictates sprite behavior.
Real-Life Example
Just as you coordinate actions in real life, Variables works by structuring logic commands in sequence:
- 1 Register the trigger event block.
- 2 Run calculations or movements within a continuous control block.
- 3 Update values or graphics on the stage viewport.
Key Highlights:
- Trigger event
- Logic evaluation
- Visual update
Interactive Diagram
Launch the interactive diagram to see this in action.
Open Interactive DiagramThe interactive diagram for this chapter demonstrates Variables. It shows variables being created, set, changed, and displayed on stage with their values updating.
What to explore:
- create a variable; set its value; watch it change as blocks run; see the variable monitor update on stage
- variables store and track values that can change during a program — like a score in a game that increases when you collect items
Introduction
Imagine playing a game that never told you your score. You would have no idea if you were winning or losing. Imagine an animation that could not count how many times a character jumped. Variables are how programs remember and change information. They are like labeled storage boxes where you can keep numbers, text, or true/false values and update them as your program runs.
Variables are one of the most important concepts in all of programming. They allow your program to have memory — to remember past events, track progress, and make decisions based on accumulated information. In Scratch, Variables can store numbers (like a score), text (like a player name), or Boolean values (like whether the game is over).
In this chapter, you will learn what Variables are, how to create and use them in Scratch, and common variable patterns like scoring, timing, counting, and toggling. By the end, you will understand how Variables give your projects memory and the ability to respond to changing conditions.
How It Works
To create a variable in Scratch, click the Variables category and then 'Make a Variable'. You give it a name (like 'score' or 'lives') and choose whether it applies to all sprites (global) or just the current sprite (local). Once created, you get several blocks: 'set [variable] to ()' (assign a value), 'change [variable] by ()' (add or subtract), and 'show variable' / 'hide variable' (display or hide the variable's monitor on stage).
Variables can store different types of data. Number Variables store numeric values and can be used in arithmetic. String Variables store text. Boolean Variables store true/false. In Scratch, a variable automatically adapts to whatever type of value you assign to it. You can also use Variables in operators: add them, compare them, and combine them with text.
Household Object Analogy
Think of Variables like labeled jars on a shelf. Each jar has a name (like 'Score' or 'Player Name'). You can put something in the jar (assign a value), take something out (read the value), or change what is inside (update the value). You can also look at the jar from across the room if the stage monitor is showing. Just as jars hold physical items, Variables hold data.
Deeper Dive
Variable scope determines which sprites can access a variable. 'For all sprites' (global) Variables can be read and changed by any sprite and the stage. 'For this sprite only' (local) Variables can only be accessed by the sprite that created them. Use global Variables for things like score, level, and game state that affect the whole project. Use local Variables for sprite-specific data like individual enemy health or position offsets.
Variable monitors can be displayed on the stage in three modes: normal (shows the variable name and value), large (bigger text for readability), and slider (a slider that lets you change the variable by dragging). Slider mode is extremely useful for testing — you can adjust values like speed, gravity, or difficulty in real-time without editing code. Right-click a variable monitor to change its display mode and range.
Cloud Variables are a special type of variable in Scratch that are stored on the Scratch server and shared across all users of a project. They enable multi-player games and persistent data. Cloud Variables have restrictions: they can only store numbers, they have a maximum value, and they update relatively slowly. They are identified by a cloud icon next to their name in the blocks palette.
Key Insight
Variable names matter. Good variable names like 'playerScore', 'remainingLives', or 'isGameOver' make your code self-documenting — anyone reading your project can understand what each variable represents. Avoid names like 'a', 'x1', or 'temp' that do not describe the variable's purpose.
Advanced
Variables in Scratch are dynamically typed — they can hold any type of value and change type as needed. This is different from languages like Java or C++ where you must declare a variable's type (integer, string, boolean) and cannot change it later. Dynamic typing makes Scratch easier for beginners but can lead to unexpected behavior if you accidentally store a text value where you expected a number.
The 'set [variable] to (join [hello] [world])' block, combined with 'set [variable] to ()', allows for string manipulation. You can build messages, create dynamic text displays, and format output. For example, you might set a 'message' variable to (join [Score: ] (score)) to display 'Score: 100'.
Lists are Variables that can hold multiple values. Think of a list as a row of numbered boxes, each containing a value. Scratch lists support add, delete, insert, and replace operations. Lists are essential for managing collections of data — inventory items, high scores, enemy positions, or dialogue sequences. Lists are created and managed in the same Variables category.
Vocabulary Table
| Term | Definition |
|---|---|
| Variables | The primary technological concept explaining how components interact within the context of Scratch Programming. |
| Coordinate Grid | The X and Y plane representing the Stage layout coordinates. |
| Hat Block | A block with a rounded top that registers events to launch scripts. |
| Sensing Block | A block that checks for collisions, touch parameters, or mouse pointer coordinates. |
Fun Facts
The name 'variable' comes from mathematics, where a variable is a symbol that can represent different values. In programming, Variables serve the same purpose but are stored in computer memory.
Scratch supports up to 200 Variables per sprite and 200 global Variables per project. This is far more than you will ever need for most projects.
Cloud Variables were introduced in Scratch 2.0 and are only available to Scratchers (signed-in users) with verified email addresses.
Variable monitors in 'slider' mode can be set to any numeric range, such as 0 to 100 or -50 to 50, making them useful for interactive testing.
The 'show variable' and 'hide variable' blocks let you control whether a variable's monitor appears on the stage at runtime. Hiding monitors keeps the stage clean while the project runs.
Common Misconceptions
Misconception: Variables can only store numbers.
Truth: Variables in Scratch can store numbers, text (strings), or true/false values. They automatically adapt to whatever value you assign.
Misconception: Creating more Variables makes your project run slower.
Truth: Variables have minimal impact on performance. Creating dozens or even hundreds of Variables will not noticeably affect speed.
Misconception: Local Variables and global Variables work the same way.
Truth: Local Variables are only accessible by the sprite that created them. Global Variables can be accessed and modified by any sprite. Use local Variables to prevent unintended interference between sprites.
Misconception: Cloud Variables work exactly like regular Variables.
Truth: Cloud Variables have limitations: they only store numbers (not text), have a maximum value of 2.7 billion, update every few seconds, and require a Scratcher account to use.
Knowledge Check
1. What is the main role of Variables?
Answer: To orchestrate behaviors and controls visually
2. Which block shape starts a script stack in Scratch?
Answer: Hat block (curved top)
3. What is the maximum X coordinate limit on the stage?
Answer: 240
