Variables in Hopscotch
In this article, we will explore the four different types of variables in Hopscotch. Here is what you will learn:
- What is a variable?
- What is scope?
- What is a declaration?
- What are the different types of variables in Hopscotch?
- Game variables
- User variables
- Object variables
- Local variables
What is a Variable?
A variable stores information. This can be a number or some text information, for example. This is referred to the value of the variable.
Keeping this information in a variable makes it useful for objects to be able to perform calculations and share information with each other.
For example, you can create a variable to keep track of the score within your game. Then you can make the score increase, such as after a character bumps into a coin, or attacks an enemy.
What is Scope?
Scope is where in the code an object can access the value of a variable.
Continuing from the previous example: where other objects — such as a character, a coin, another Text object — are able to access the "Score" variable in different parts of the code, is what is referred to as scope.
The scope of the "Score" variable is set to the entire project, so all other objects within the project can access it.
Both Chillanna and the Score Display objects can access it. Chillanna can increase the value of "Score," and the Score Display text object can set its text to "Score."
What is a declaration?
When you declare a variable, you set the value of a variable, by using the Set Variable block, for the first time. If you create a variable in the Variables keyboard without declaring its value, the variable is set to 0 by default.
In this example, Gorilla draws a grid of dots. You can change the number of rows, columns, and the spacing between the dots, by changing the declarations of the "Rows," "Columns," and "Grid spacing" variables. Here is the project link: https://c.gethopscotch.com/p/121k0649o2
What are the different types of variables in Hopscotch?
In Hopscotch, there are user variables, game variables, object variables, and local variables.
- Game variables can be accessed by all objects in the project and are the same value for every object in the project.
- User variables are game variables that persist between plays and exist for all objects in the entire project. They have the same value for each user who plays the project until cleared.
- Note: User variables are available only with the Hopscotch subscription.
- Object variables are unique for each object. For example, each object will have its own X position, Y position, etc.
- Object variables have a bubble that let you choose which object it is referring to.
- "Original Object" means that this variable is the same value for the original object and all of its clones, and can be accessed by the original object and all of its clones.
- "Self" object variables are unique to each object, and also for each clone of an object. Objects cannot access each other's self variables.
- Object variables have a bubble that let you choose which object it is referring to.
- Local variables exist within a container. You can specify which container it exists in by changing where you declare it.
- For example, the "Bounce height," "Horizontal velocity," and "Vertical velocity" local variables have been declared within this "Bounce" custom rule. So they will exist only inside this custom rule.
- To learn more about local variables specifically, check out our article on Local Variables.