|This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites.
- This article is about Scratch variables in general. For other uses, see Variable (disambiguation).
A variable is a changeable value recorded in Scratch's memory. Variables can only hold one value at a time, unlike lists. These values can be either numbers or strings. Clicking on an isolated variable in the scripts area displays a small bubble reporting the value of the variable. Unlike many other programming languages, Scratch does not allow variables to be created by a script as it runs. Instead, variables are created with the "Make a Variable" button in the Block Palette. Lists allow multiple values to be stored, acting like a group of variables.
There are five different blocks relating to variables:
|Click on a block to find out more.
(variable) // This is the block that reports the value of a variable
set [variable v] to (0) // This sets a chosen variable to a specified value
change [variable v] by (1) // This changes the value of a chosen variable by a specified amount
show variable [variable v] // This shows the specified variable in the project player
hide variable [variable v] // This reverses the 'show variable' block
There are three types of variables. All variables are stored in RAM, and default to the values in the file the project originates from (project.json). Variable changes are saved with the project and do not reset on green flag.
Global variables are created by selecting "For all sprites" in the variable creation menu. They can be read and changed by any sprites, clones, and the stage. Opening the project.json file in the SB3 shows that global variables are stored by the stage.
Local (also known as private or personal) variables are created by selecting "For this sprite only". Local variables are stored independently by each sprite or clone can only be changed by their owner, but can be read by other sprites using the () of () block. The stage cannot have local variables; any local variables in code dragged into the stage will be recreated as global variables, with a prefix of "Stage:".
Clones inherit local variables into their properties, meaning that each clone has a separate number for the local variable. The clone will still keep the value of the variable, but it can change it. This is also useful in the same way.
- Main article: Cloud Data
Cloud variables are created by selecting "Cloud variable (stored on server)". They are stored on Scratch's server. When a cloud variable updates, it does so across all instances of the project open, and it also gets saved for the next time the project is opened. Cloud variables have a small cloud icon next to their names. In Scratch 3.0, cloud variables only support numerical data and all are limited to 256 digits. Additionally, a project can have a maximum of 10 cloud variables, after that the cloud variable checkbox is greyed out. Finally, cloud variables can only update once every 0.1 seconds. While it is possible to make chat rooms with cloud variables, it isn't allowed because it would be too hard to moderate.
- Main article: What can I use variables for?
Variables are used whenever a value must be stored — e.g., if a project required the user to input a name and then remember that name, the name would be stored in a variable. With this, the name can be retrieved at any time; all the project has to do is check the value (which is the name).
|This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.
Glitch variables could be made by naming them with names including %d (direction drop down/number insert), %s (text input), %m (variable drop down), %b (boolean), %n (number input), or %c (color input), which will look like those at the left. This is because Scratch reads the percent sign followed by certain letters as an argument. By naming a variable @greenFlag or @stop it will display a green flag or a stop sign, respectively, as seen at the right. Also, the codes @turnRight and @turnLeft create a rotate sign, right or left.
This bug was patched in v423 of Scratch 2.0 and no longer works.
- Main article: Variable Monitor
The look of the variable monitor can be changed into three forms: the normal readout, the large readout, and the slider. The form of the variable can be changed by double-clicking or right-clicking it and selecting the option that is wanted.
The normal readout contains the title and the value, the large readout only contains a value, and the slider is similar to the normal readout, but has a slider on it. If the slider is chosen, there will be another option once right-clicked called "Change slider range." Choosing it will allow the user to set the range of the slider.
It can be chosen whether to hide or show a variable on the stage by clicking the tick box in the variable panel.
- Main article: List
A list is made of items — each item like a variable. Lists can be useful when many variables are needed or when the amount of memory that needs to be stored can not be determined by the programmer before the project is run. However, lists can only store 200,000 items.
Cloud lists do not exist in Scratch 3.0. They have been suggested many times and were rejected by the Scratch Team. Cloud lists, if they existed, would be accessible by all Scratchers via the cloud server.
Some Scratch Modifications contain cloud lists. Some projects also contain cloud lists, although they are very hard to make and most of them usually break in new versions of Scratch. The only way to make one is to encode and decode lists into and from cloud variables, but this is very limited because variables are restricted to 256 digits.
Variables are used in numerous projects to keep score, show health, connect with players, or list some friends. Here are some projects that use variables:
- griffpatch's "massive multiplayer platformer" game uses cloud data to connect players.
- MooShoeGaming's "The Front" project used variables as "True or False" statements to tell if the player is still alive.
- djpro's guide to making platformer games has to have variables for the ball to move and jump as if gravity was affecting it.
Those were only a few examples of variables used in programs; variables are used widely in different projects around Scratch, and most more advanced projects use them.
- myhf. (5/5/2015). "A variable can hold up to 10240 unicode characters." post:993624
- Za Chary. (19/3/2019). "...the Scratch Team will not be adding an easy official way to make cloud lists." topic:343602