Kodeclik Blog

Variables in Scratch

Scratch is a favorite programming language for kids because it is so easy to get started with coding by just stacking blocks, something kids are adept at! There are numerous blocks in Scratch for movement, drawing, and control but every once in a while there will come a situation when you will need Scratch to mimic the capabilities of regular programming languages. One such capability is the feature to create and use variables.

What is a variable?

In the Scratch programming language, a variable is a named container that stores a value. It allows you to keep track of and manipulate data within your program. You can think of a variable as a labeled box that holds a specific value, such as a number, text, or Boolean (true/false). For instance you can have a variable called “name” and it can store a name, e.g., “Mickey”. You can have a variable called “Day” and it can store the day of the week, e.g., “Wednesday”. You can have a variable called “Temperature” and it can store the temperature such as “75” (It is summer!)
Variables in Scratch are dynamic, which means their values can change as the program runs. You can assign a value to a variable using blocks like "set [variable] to [value]" or "change [variable] by [amount]".
To create a variable in Scratch, you can use the "Make a Variable" block, which allows you to give the variable a name and an initial value. You can also use the "For this sprite only" checkbox to specify whether the variable should be local to a specific sprite or global, accessible by all sprites in the project.

Example 1

For instance, here is a simple Scratch program that has one Sprite, a basketball. The main program for the basketball simply bounces the ball endlessly. Each time it hits the boundary, we update a variable called “Boundaries”. Try it out!
The above code sets the variable to zero at the start of the program and moves the ball ten steps forever. If the ball hits the edge, it bounces, turners 30 degrees and the variable gets changed by one.
Here we see the game being played with a score of 121!

Example 2

As another example, here is a Scratch program where the Sprite asks you for your age. This age is stored in a variable called “Age”. Then it adds 10 to the variable and prints a message giving your age in 10 years. Note that the value assigned to the variable has changed.
The program sets the variable “age” to zero at the start and then asks the player for their age. After that it sets age to the answer and adds ten. Finally it says what age the player will be in ten years!
Here the scratch cat says that the player will be 47 in 10 years. What age is the player now?
Variables in Scratch are thus useful for different purposes, such as performing calculations, storing user input, keeping score, or controlling program flow. Variables provide flexibility and allow you to create dynamic and interactive programs in Scratch.
In Scratch, there are actually three types of variables: local variables, global variables, and cloud variables. Each type has a different scope and purpose within a Scratch project.
Local variables are specific to a particular sprite or script within a project. They are created and used within a single sprite or script and are not accessible from other sprites or scripts. Local variables are useful for storing temporary or intermediate values that are relevant only within a specific context. Once the sprite or script finishes executing, local variables are destroyed and their values are lost.
Global variables, on the other hand, are accessible from any sprite or script within a project. They are created using the "Make a Variable" block from the "Data" category and selecting the option "For all sprites." Global variables retain their values across different parts of the project and can be read from or modified by any sprite or script. Global variables are useful for storing information that needs to be shared and accessed by multiple sprites or scripts. For instance, the running game score should be a global variable if multiple sprite actions can lead to changes in its values.
Finally, cloud variables are a special type of variable that can be shared and synchronized across different Scratch projects running on different devices. They allow for collaborative and interactive experiences where multiple users can interact with and modify the same variable values. Cloud variables are stored on Scratch's servers, and changes made to a cloud variable by one user can be seen by others in real-time. Cloud variables are particularly useful for creating multiplayer games, online collaborations, or shared leaderboards.
To create a cloud variable, you need to enable cloud data in your project settings. Then, you can use the "Make a Variable" block and select the option "For this sprite only" to create a cloud variable. Cloud variables can be read, written, and monitored using specific blocks provided in the "Data" category, allowing for synchronized data sharing across multiple Scratch projects.
Remember that global and cloud variables can have the same name, but they are distinct from each other. Global variables are shared within a project, while cloud variables can be shared across different projects and devices.
So in summary, a variable can store different types of data such as numbers, strings, Boolean values (true/false), or even more complex data structures. They can be created and named by the user, allowing for customizability and flexibility in programming. They provide a way to store and manipulate data dynamically, making your Scratch projects more interactive and engaging. They are particularly useful for keeping score, tracking user input, or managing game states. For example, see how to create a Clicker Game in Scratch where we use a variable to keep track of the number of clicks. Finally, remember that there are three types of variables, namely local, global, and cloud, and make sure to use the right one for your application!
If you liked this blogpost, learn how to make a clicker game in Scratch.
Want to learn Scratch with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2023. All rights reserved.