ScratchLearningResource VariablesandLists
ScratchLearningResource VariablesandLists
Have you used Variable or List blocks in a Scratch project? They can be a powerful tool when you
are creating customized programs. Perhaps you have used a variable to store a game score*, but
did you know a variable can hold numbers or text (also known as a “string”)? And if you are storing
a lot of custom information, using a list may be more efficient than creating multiple variables. Let’s
explore what variables and lists are, and see a number of ways they can be used.
*See our in-editor tutorials or coding cards for instructions on how to set up a basic score.
A list, on the other hand, can store multiple pieces of information, all
ordered as separate items. A list is also called an “array” in other
programming languages.
The information stored can change the direction of a game or story, turn
on and off actions, signal special messages, and more!
You can then use a conditional “if then” statement to show one
response if their answer is correct and a different response if the
answer is incorrect.
holds or the value it reports. You can also check a box next to many
of these reporter blocks to display them on the stage via a stage
monitor.
You could rename it or delete it by right-clicking on it. Or click the “Make a Variable” box to
create a new one. When the “New Variable” text box appears, type the name of the information you
want to store in the text box for the name.
You can then choose if all sprites will have access to the variable, or only the current one
you’ve selected. Many times, you’ll probably choose to leave it as the default “for all sprites,” so
any sprite in your program can change the variable, reset the variable, or check the data the
variable currently holds (for instance, an overall game score). However, there may be times where a
variable only applies to one sprite. For example, when different sprites might have their own health
meter in a game. If you opt to use Cloud Variables, they are stored “in the cloud” or on Scratch’s
servers so the values are preserved when projects are reloaded.
Notice the different types of blocks available in the Variables category. There are blocks to set or
change the variable, for instance, and even a variable reporter block (that oval block) that can be
used inside another block.
Using Variables
Let’s create a project where the variable controls the brightness of
the sprite. You’ll see there is no reporter block for brightness
available in the block palette. Create a variable called “brightness”
and check the box next to your custom “brightness” variable in the
block palette so it will be shown on the stage. Now, right-click its
stage monitor and you’ll see you have a few options: “normal
readout,” “large readout,” and “slider.” Let’s make this a slider,
so the user can control the sprite’s brightness.
And what if you want to have the backdrop’s brightness be the opposite of the sprite’s? You could
make a black box as your backdrop costume and then create a script that says the backdrop
should forever set the brightness effect to 100 minus what is in the “brightness” variable. Test again
to see how this works. The brightness of the backdrop and the sprite should be opposite. What
else could you try?
Slider Variables
Sliders can be handy for other things, too, like speed or volume.
If you want to set limits on the range of numbers available in the slider, you can right-click on the
variable’s stage monitor to set a “slider range” minimum and maximum.
For instance, you could create a project with code like the one below, where the slider variable
controls the speed of the spin by setting the “spin” variable as the number of degrees to turn.
If it spins too fast, perhaps that makes you dizzy, so you may want to set a lower maximum
number for the slider.
A list could be used to store multiple pieces of related information, or it could be used in place of
creating multiple variables.
For example, say you want to create a project that collects the user’s answers to multiple
questions. Each answer could be passed and stored in individual variables by using the “set
[variable] to” block to pass the “answer” into a variable after each related question is asked. This
requires setting up multiple variables.
This code stack shows passing data from the “answer” This code stack shows passing data from the “answer”
reporter into different variables after each question is asked. reporter into a single list after each question is asked.
Using Lists
You can check a box next to the list in the block palette to display it on
the stage via a stage monitor. When visible on the stage, you can
right-click on the stage monitor and choose to export data from the list
to a file on your computer if you want to save it. Or you can import data
into a list (from a .txt or .csv file), if you need to quickly add a lot of
information at once. You can also click the plus (+) sign in the lower left
When you create a Scratch project and populate a list, the information in the list will be
retained until you tell the program to delete items from the list or delete the entire list.
The state of your project is preserved when you save it, meaning if there is information in your list,
you do not need to tell the program to add those same items to your list each time it is run, unless
you have deleted them from the list and they need to be re-added.
When a list has been populated, click on the list reporter blocks to see the value each holds.
There are a variety of simple and complex uses for lists, such as:
● using a predefined list to determine animation (like a melody project)
● performing calculations (like determining a sum of numbers in a list)
● storing inventories in games (to check what items have been collected or can be collected)
● encoding and decoding messages/strings (like an alphabet to binary code converter)
● recording multiple values like the movement of a mouse or sprite around the stage (like in a
fruit slicer game or in a position recorder project)
● creating a system for users to type letters or display words/closed captions on the stage
(like in a Mad Lib project)
● programming a randomized prompt idea generator (utilizing multiple lists full of adjectives or
categories, for when you need ideas about what kinds of projects to create) or a simple
version where the program picks a random item from a list of ideas
These are just a few of the possibilities. Look through all the blocks available for lists. What do you
think each does? How might you use some of the blocks in a program to add, edit, or reset the
data in a list? Experiment! How will you use variables and lists to create your own customized
program?
Example project where the program picks a random item from a list of ideas (https://scratch.mit.edu/projects/962298830).
Example project where the program performs calculations on numbers in a list (https://scratch.mit.edu/projects/286059732).
Example project where the program records the positions of a mouse (https://scratch.mit.edu/projects/962767941).