Before reading about local variables, check out our article on Variables in Hopscotch first. That article contains information on the different types of variables in Hopscotch. It also includes definitions for words mentioned in this article, such as declaration and scope.
This article contains information on
- Creating/Using Local Variables
- Setting the Scope of Local Variables
- Changing the Scope of Local Variables
- Merging Local Variables
Creating/Using Local Variables
We have a small demo project to illustrate using local variables: https://c.gethopscotch.com/p/121k0649o2
In this project, Gorilla draws a grid of dots. You can change the values of the "Rows" and "Columns" local variables to change how many rows and columns the grid has. This will make the grid bigger or smaller, and you can change the distance between the dots by changing the value of the "Grid spacing" local variable.
In this case, the advantage of using local variables are:
- You can change the numbers quickly and easily in the variable declaration statement.
- For example, without the "Grid spacing" variable, if you wanted the dots closer to each other, you would have to check every line of code for where the number 50 is used and manually change it to a smaller number (let's say 20).
- However, with the "Grid spacing" variable, you can simply change the declaration of the "Grid spacing" variable to be 20.
- Local variables exist only in the Variables keyboard for code where the grid is drawn.
- This means that they will not exist for other objects that are not drawing a grid, and will not show up in the Variables keyboard when you are editing different rules where Gorilla is not drawing the grid. This can help you to avoid clutter that is not relevant.
Creating Local Variables
To create a local variable, drag out a "Set Variable" block. The first blank bubble of the "Set Variable" block will automatically be selected, and the Variables keyboard will pop up.
Under the "Local" section, you can tap "New Variable" to create a local variable. Choose a name for it, and then press "done." It will start with a value of 0 by default until you set it to be another value.
Setting the Scope of Local Variables
The first time you set the value of a local variable using the "Set Variable" block is called declaring the variable. Where the local variable is declared will determine its scope, meaning where an object can access a variable in code.
The scope of the local variable will be the container where it is declared. A container is a block of code in Hopscotch — for example, a pink "When" container, a custom rule or custom ability, a "Repeat" block, a "Check Once If" block, or a "Draw a Trail" block.
Changing the Scope of Local Variables
Currently, when you drag the "Set Variable" block of a local variable - where it is declared - inside or outside of a specific container, you can change the scope of that local variable to the new container.
In the example project, the "Rows," "Columns" and "Grid spacing" local variables will exist in the "When Game Starts" container, where they are declared.
The local variable exists in the container where you declare it and any other containers inside. In this case, the local variables also exist in the "Repeat" containers inside the "When Game Starts" container, where they were declared.
The local variable will appear in the Variables keyboard where it exists. If a local variable is not used anywhere in the current scope, it will not appear on the Variables keyboard. If you want to use the local variable again, you can create another local variable with the same name.
Local variables will reset each time their scope is reset. For example, if you create a local variable inside a "When __ is tapped" container without declaring it, it will reset to its default value 0 each time the object is tapped. If you create and use a local variable inside a "Repeat" container without declaring it to a specific value, it will reset to 0 each time the code is run.
Exceptions for Setting Local Variable Scopes in Hopscotch
Custom rules and custom abilities are exceptions to setting/changing local variable scopes — a variable declared in an outer container will not exist inside the custom block. In other words, only the local variables declared within a custom rule or a custom ability can be accessed inside the custom rule or custom ability.
This is because custom blocks can be reused in different parts of the code, where the local variable may have not been declared. In future, custom blocks will have parameters to allow local variables within them to be declared at different values.
Merging Local Variables
When local variables are used in different containers of an object, they are distinct, even with the same name, since they have different scopes.
It is possible to merge local variables in different When containers, so that they refer to the same value. Here is how to merge local variables:
- Use local variables with the exact same name, in different When containers
- Declare the variable using the "Set Variable" block outside the When containers
Example with "Health" variable
In this example below, the local variable "Health" is the same variable used in the "When game is playing" rule and the "When Self bumps Spike" rule.
The two "Health" variables have the same name in both rules. Because the "Set Variable" block is declared outside the two When containers, they have been merged. Without the declaration on top, the two "Heath" variables would have their own values, and changing one would not affect the other.
Merged local variables, outside of the When containers, are similar to object variables, but they will exist only for an object when used. So an object will have the "Health" variable only when it uses this code — rather than "Health" always appearing in the Object Variables section of the Variables keyboard, for all objects.
Example with the "Bounce" custom rule
For another example of merged local variables, check out the "Bounce" custom rule below. The "Bounce height," "Horizontal velocity" and "Vertical velocity" local variables are merged local variables, since they have been declared outside the When containers, and have the same name in each of the rules where they have been used.
The "Bounce height," "Horizontal velocity" and "Vertical velocity" local variables will appear in the Variables keyboard only for objects that use the Bounce code, rather than appearing in the Variables keyboard for all objects.
Previously, the "Bounce" custom rule used object variables, which meant that all objects would have the "Horizontal velocity" and "Vertical velocity" variables even if they didn't use the "Bounce" code.
Local variables behave differently from other Hopscotch variables, which can't have the same name. Local variables can have the same name when used in different scopes, and will need to have the same name to be merged.