Variables
A variable holds a value that changes during the game: score, lives, time, the player's name.
Types
| Type | Holds | Example |
|---|---|---|
| Number | Numeric values | Score = 10 |
| Text | Words | Name = "Ana" |
| Boolean | True or false | HasKey = true |
Where they live
| Scope | Valid for | Where to create |
|---|---|---|
| Global | The whole game, in every layout | Global variables, in the Project panel |
| Layout | Only that layout | Layout variables panel (left) |
| Instance | Each instance of the object, with its own value | The object's Variables tab |
Global variables show up as green rows at the top of every event sheet. Double-click a row to edit the initial value.
Instance variable example: create
Healthon theEnemyobject with value 3. Each enemy has its ownHealth: hitting one does not hurt the others.
Changing a variable
Use the Change variable value action (under System → Variables, or on the object itself for instance variables). Pick the variable, the operation and the value:
| Operation | Effect |
|---|---|
= |
Sets the value |
+ |
Adds |
- |
Subtracts |
×, ÷ |
Multiplies, divides |
| Conditions | Actions |
|---|---|
Player Collision with Coin |
Change variable value Score + 1 |
Comparing a variable
Use the Variable value condition (System → Variables) to test the value:
| Conditions | Actions |
|---|---|
Variable value Score >= 10 |
Change layout to Victory |
Variables in expressions
Any numeric field accepts an expression. A variable's name stands for its value: Score * 2, Health - 1. For instance variables, write Enemy.Variable(Health).
To join text and a number, use ToString:
"Score: " + ToString(Score)
Tips
- Declare the variable before using it. A global variable has to exist in the list.
- Use names without accents or spaces.
- To keep a high score between runs, see local storage in Tips.