Instance variables example
The instance variables example displays the usage of instance variables.
Have a look at the explanation in the layout as well as the event sheet.
You can press F5 to preview the project in your browser and execute it as if it was exported.
In Construct you start by adding an object type to your project (for example a Sprite).
Then you can create instances of this object type during edit time or even during the execution of the game.
The object type may hold variables, which can hold a number, some text, or a boolean (on/off flag) for individual instances. These are instance variables.
Instance variables are common to all instances of an object type.
The object type is Sprite, we have 5 instances of this object type in our layout. Sprite has an instance variable named “ClickCount”.
All the instances of Sprite do have this instance variable.
Each instance of Sprite has its own value in “ClickCount”.
There is also a Text object type that has 5 instances, placed on top of each instance of Sprite.
If you click three times on the Sprite instance on the left, it will display “3” in the associated Text instance on top it.
This works because there are as many instances of Text as there are of Sprite and they were created and positioned in the same order.
Our first instance of Sprite holds the value “3” whereas all the other instances, at this time hold the value “0”.
In this case, the instance variable is a numeric value (actual numbers that can be used in maths operations).
Variables can also hold text values - also known as a “character string”.
Strings can contain numbers, but unlike numeric values, those are considered as characters, as text, and cannot be used as is in an operation.
The last type of value for a variable is boolean. The value is either True or False.
In that case “True” or “False” are not text like character strings but are known as reserved keywords.