From Test-Scratch-Wiki
- This eng is about Scratch variables in general. For other uses, see Variable (disambiguation).
- "Value" redirects here. For the category of values, see Category:Values.
This page has links to outside of the Scratch website or Wikipedia. Remember to stay safe when using the internet as we can't guarantee the safety of other sites. |
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
— any text. Clicking on an isolated variable in the scripts area displays a small bubble reporting the value of the variable. Unlike many other programming languages, variables must be created prior to when the project actually runs. This only results in a small amount of RAM being used to store the value for use when the project actually runs. Variables are created with the button in the Variables palette.
Variable blocks
There are five different blocks relating to variables:
- () (this is the block that contains the name of the variable, which reports the value)
- Set () to ()
- Change () by ()
- Show Variable ()
- Hide Variable ()
Types
In Scratch 1.4, there are two types of variables, public (global), and private (local). In Scratch 2.0, another type was added — cloud. Cloud data variables are stored on the server, making their same values accessible by all users viewing the project.
Global Variables
By default, when a variable is created, it is a global variable. Global variables can be read and changed by any sprite, or the Stage. All variables are stored in RAM, and default to the values in the .sb or .sb2 file.
Local Variables
Local, (or private/personal) variables are created the same way as global, but another option is selected in the variable creation dialog, "For this sprite only". Personal variables can only be changed by their owner, but can be read by other sprites using the () of () block. The Stage cannot have local variables.
Local variables are extremely useful when you want to create a template sprite which needs to be duplicated and edited. For example, in a game where one must pop bubbles, a "bubble" sprite should be made which has personal variables like "speed" and should be programmed independently. Then it should be duplicated until there are enough bubbles. Since each bubble has an individual "speed" variable, they will not interfere with each other unlike if "speed" was a global variable.
Note: | Clones inherit local variables into their properties, meaning that each clone has a separate number for the local variable |
Cloud Variables
- Main article: Cloud Data
Cloud variables are variables that are stored on the server, though they can only store numbers at the moment. When a cloud variable updates, it does so across all copies 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.
Use
- Main article: What can I use variables for?
Variables are used whenever a value must be stored
— i.e., 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).
The look of the variable can be changed in to 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, or clicking it using the grow/shrink sprite tool.
It can be chosen whether to hide or show a variable on the stage by clicking the tick box in the variable panel or by right-clicking it and selecting "hide".
If the view is set to slider, right clicking on the watcher will give another option: set slider min and max. Choosing it will let the user define the range of the variable.
Variable Glitches
You can make glitch variables by calling them things like %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.
These tricks only work in Scratch 2.0. This bug was patched in v423 of Scratch and no longer works.
Lists
- 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.
Examples
Variables are used in a ton of projects to keep score, show health, connect with players, or list some friends. Here are some projects that use variables:
https://scratch.mit.edu/projects/46587498/ - griffpatch's multiplayer "Terraria" game uses cloud data to connect players
https://scratch.mit.edu/projects/52484548/ - LBMCompany's virtual telescope project uses variables to tell how many stars there are on the screen
https://scratch.mit.edu/projects/26547147/ - MooShoeGaming's "The Front" project used variables as "True or False" statements to tell if the player is still alive.
https://scratch.mit.edu/projects/34429446/ - 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.
See Also
External Links
- Variable on Wikipedia
- Input Type Specifiers