Skip to content

Variables and Data in Scratch

Variables are like your sprite’s memory - they help your program remember important information like scores, names, or the number of lives left in a game. Think of them as labeled boxes where you can store different types of information!

A variable is a container that holds a value that can change during your program. Just like a box with a label, you can:

  • Put information in (set the value)
  • Look at what’s inside (use the value)
  • Change the contents (update the value)
  • Use the contents in calculations (mathematical operations)

🎮 Game Mechanics

Track scores, lives, levels, or any game state that changes as players progress through your creation.

📊 Calculations

Store numbers for math operations, count how many times something happens, or track measurements.

💬 User Information

Remember player names, answers to questions, or choices made during interactive stories.

🎨 Dynamic Behavior

Control sprite behavior based on stored values - make characters smarter and more responsive!

Let’s learn how to create and work with variables step by step:

  1. 🏷️ Create a New Variable Go to the Variables category and click “Make a Variable”. Give it a descriptive name like “score” or “player_name”.

  2. 📝 Choose Visibility Decide if it’s “For all sprites” (global) or “For this sprite only” (local). Most game variables are global.

  3. 🎯 Set Initial Values Use “set [variable] to (value)” blocks to give your variable a starting value.

  4. 🔄 Update as Needed Use “change [variable] by (amount)” to increase or decrease values, or “set” to replace completely.

  5. 📺 Display if Helpful Check the box next to your variable name to show it on the stage - great for scores and timers!

set [score ▼] to (0) - Sets the variable to exactly this value
change [score ▼] by (10) - Adds (or subtracts) this amount
set [name ▼] to (answer) - Can store text, not just numbers

Here are essential patterns you’ll use in almost every project:

when green flag clicked
set [score ▼] to (0)
when [collectible ▼] clicked
change [score ▼] by (10)
play sound (coin ▼) until done
if score = 100 then
say [You win!] for (3) seconds
end
when green flag clicked
set [lives ▼] to (3)
when I receive (player hit ▼)
change [lives ▼] by (-1)
if lives = 0 then
say [Game Over!] for (3) seconds
stop [all ▼]
end
when green flag clicked
set [time ▼] to (60)
repeat until time = 0
wait (1) seconds
change [time ▼] by (-1)
end
say [Time's up!] for (2) seconds
when green flag clicked
ask [What's your name?] and wait
set [player_name ▼] to (answer)
say (join [Hello, ] (player_name)) for (3) seconds

Lists are like variables that can hold multiple pieces of information at once - imagine a shopping list or a high score table!

  1. 📋 Make a List In Variables, click “Make a List” and give it a name like “high_scores” or “inventory”.

  2. ➕ Add Items Use “add (item) to [list ▼]” to put new information at the end of your list.

  3. 🔍 Access Items Use “item (1) of [list ▼]” to get specific items. Lists start counting from 1, not 0!

  4. 🗑️ Remove Items Use “delete (1) of [list ▼]” to remove specific items or “delete all of [list ▼]” to clear everything.

  5. 📊 Get Information Use “length of [list ▼]” to find out how many items are in your list.

when green flag clicked
if length of [high_scores ▼] = 0 then
add (0) to [high_scores ▼]
end
when game ends
if score > item 1 of [high_scores ▼] then
replace item (1) of [high_scores ▼] with (score)
say [New high score!] for (3) seconds
end
when [treasure ▼] clicked
add (treasure name) to [inventory ▼]
say (join [Found: ] (treasure name)) for (2) seconds
when [use item ▼] clicked
if length of [inventory ▼] > 0 then
say (join [Using: ] (item (1) of [inventory ▼])) for (2) seconds
delete (1) of [inventory ▼]
else
say [No items to use!] for (2) seconds
end

Scratch variables can store different types of information:

🔢 Numbers

Integers: Whole numbers like 5, -10, 0
Decimals: Numbers with decimal points like 3.14, -2.5
Perfect for scores, positions, timers, and calculations

📝 Text (Strings)

Words and phrases: “Hello”, “Player1”, “Game Over”
Mixed content: “Score: 100”, “Level 5”
Great for names, messages, and displaying information

✅ Boolean-like Values

True/False concepts: Use 1 for true, 0 for false
Status tracking: “on”, “off”, “ready”, “playing”
Helpful for game states and conditions

🎨 Special Values

Colors: Store color values for pen drawing
Coordinates: Store x,y positions as text like “100,50”
Complex data: JSON-like strings for advanced projects

when green flag clicked
set [difficulty ▼] to (1)
set [enemy_speed ▼] to (2)
every (30) seconds
change [difficulty ▼] by (1)
set [enemy_speed ▼] to ((difficulty) * (2))
when I receive (player action ▼)
if player_level < 5 then
say [Great job, beginner!] for (2) seconds
else
say [Excellent work, expert!] for (2) seconds
end
// For shared projects only
when green flag clicked
if ☁ global_high_score < local_score then
set [☁ global_high_score ▼] to (local_score)
end
when green flag clicked
ask [Easy (1) or Hard (2) mode?] and wait
set [game_mode ▼] to (answer)
if game_mode = 1 then
set [enemy_count ▼] to (3)
else
set [enemy_count ▼] to (8)
end

Common problems and solutions when working with variables:

  1. 👀 Show Variables Temporarily Check variable boxes to see their values change in real-time during testing.

  2. 💬 Say Variable Values Use “say (variable) for (2) seconds” to check values at specific moments.

  3. 🎨 Use Different Display Styles Right-click stage variable displays to choose sliders, large readouts, or hidden displays.

  4. 📝 Add Debug Comments Use comment blocks to remind yourself what each variable is supposed to do.

  • Use descriptive names: “player_score” not “var1”
  • Be consistent: If you use underscores, use them everywhere
  • Indicate purpose: “is_game_running” clearly shows it’s a status flag
  • Global variables for: scores, levels, game settings that all sprites need
  • Sprite variables for: individual character health, personal timers, sprite-specific data
  • Reset variables when starting new games
  • Clean up temporary variables you no longer need
  • Use lists efficiently - don’t create separate variables for similar data
when green flag clicked
set [correct_answers ▼] to (0)
set [total_questions ▼] to (10)
set [current_question ▼] to (1)
repeat (total_questions)
set [num1 ▼] to (pick random (1) to (10))
set [num2 ▼] to (pick random (1) to (10))
ask (join (join (num1) [ + ]) (num2)) and wait
if answer = (num1 + num2) then
change [correct_answers ▼] by (1)
say [Correct!] for (1) seconds
else
say [Try again next time!] for (1) seconds
end
change [current_question ▼] by (1)
end
say (join [You got ] (join (correct_answers) [ out of 10 correct!])) for (5) seconds

Ready to build more sophisticated programs with data?


Variables and lists are the foundation of smart, interactive programs. With Vibelf’s guidance, you’ll create projects that remember, learn, and grow with their users! 📊✨