Variables¶
Variables are data containers that allow to dynamically change their value and let the game keep track of the player's progress.
Example
A very simple use case of Variables is keeping track of the player's score. Let's say we have a named variable called score and has an initial value of 0. Every time the player picks up a star, the score variable is incremented and its value is displayed.
Types of Variables¶
Game Creator has two types of variables:
Name Variables¶
Are identified by their unique name. For example, the name score can reference a numeric variable that keeps track of the player's score value.
List Variables¶
Are identified by their 0-based index. Think of them as a collection of values, placed one after another. For example, to access the first value, use the index 0. To access the second position, use the index 1, etc...
Note all values of a List Variable are of a particular type.
Name or List?
As a rule of thumb, it is recommended the use of Name Variables. List Variables are useful when you have an unknown number of objects to choose from. For example, when locking on an enemy from a group that surrounds the player.
Scope of Variables¶
Variables can either be local or global.
Local Variables¶
Local Variables are bound to a particular scene and can't be used outside of it.
Global Variables¶
On the other hand, Global Variables can be queried and modified from any scene.
Types
Both Global Variables and Local Variables can be List or Name based.
Value Types¶
All Variables have an initial value assigned to them that can be modifed at runtime. By default, Game Creator comes with a limited number of types to choose from, but other modules might increment the amount available.
- Number: Stores numeric values. Both decimal and integers.
- String: Stores text-based characters.
- Boolean: Can only store two values: true or false.
- Vector 3: Stores an (x,y,z) vector value
- Color: Stores an RGBA color value. Can also contain HDR information.
- Texture: Stores a reference to a Texture asset.
- Sprite: Stores a reference to a Sprite asset.
- Game Object: Stores a reference to a game object.
Saving Values
It is important to note that not all data types can be saved between play-sessions. Textures, Sprites and Game Objects and not primitive types and thus, they can't be serialized at runtime.
Nested Access¶
Nested Access is a concept that allows jumping between different variables using one single command.
For example, let's say the Player object has a Local Named Variable called target
of type Game Object. This game object is dynamic but let's say the targeted object will always have another Local Named Variable called health
that contains how many hit points the enemy has.
The health
variable can be accessed using the key target/health
(with a slash). This means: Get the variable value health
that the variable target
points to.