Document stub.png This article is a stub. It may be incomplete, unfinished, or have missing parts/sections. If the article can be expanded, please do so! There may be suggestions on its talk page. (August 2018)
Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (August 2022)
Reason: Not enough examples.
A variable can be used any time a value must be stored, be it a score, name, or option. Variables can also be used to easily create text on the Stage.

Variables are very versatile, with many purposes. Some uses include keeping scores, saving inputs, checking options, and printing text. They can also be used to create easily customizable bases.

Lists can be used instead of several variables, or for an indeterminate amount of variables.

Example Uses

Score

A variable can be used to represent the player’s score in games. Most variables are used to maintain scores in cloud games. The variable in this case is usually named “score” or “points”. This is an example of increasing the score when the player hits an alien in a space-themed game:

In the alien sprite:

when green flag clicked
set [score v] to [0] // The score is reset to 0 when the game starts
forever
    if <touching (laser v)?> then
        hide
        change [score v] by (1) // The laser hit the alien so the player gains a point
    end
end

The script below congratulates the player if they reach a score of 50 or higher and then stops the project (the player won the game):

when green flag clicked
wait until <(score)>(49)>
say [Congratulations!] for [2] seconds // Runs when score is more than 49 (is 50 or more than)
stop [all v]

Changing a level

Variables can also be used for games that have levels in them such as platformers. Here is an example script:

when green flag clicked
repeat until <(level) = [5]> // The game has 5 levels, it repeats until the player gets to the 5th level
    if <touching (goal v)?> then
    change [level v] by [1] // Next level
    wait until <not <touching (goal v)?>>

Flags

Variables can be used to create "flags" Flags are used to check whether a condition is or is not completed. This is an example of unlocking an area in a game:

when green flag clicked
set [area unlocked? v] to (0)

when this sprite clicked
if <<(area unlocked?) = [0]> and <(Money) > [99]>> then
change [Money v] by (-100)
say [You unlocked the Forest area!] for (2) secs
set [area unlocked? v] to [1]
end

This code will allow the player to enter the area if they've unlocked it (if (area unlocked?) equals 1, the player can enter the area.)

when this sprite clicked
if <(area unlocked?) = [1]> then
set [area v] to [Forest]
say [You've entered the Forest area!]

end

Cloud Variables

Main article: Cloud Data

Cloud variables are similar to regular variables, except that the data from the cloud variables are stored on Scratch's Servers and they can only contain 256 digits.

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.