Game Maker Tutorial

Page 3 of 13
Variable Actions
There are two important actions to deal with variables directly, which can be found in the Control tab:
Set the value of a variable. With this action you can change the value of a given variable. This can either be one of the built-in variables or your own variable. You specify the name of the variable and the new value. When you check the Relative box the given value is added to the current value of the variable. Please note that this can only be done if the variable already has a value assigned to it! Rather than providing a simple value for the variable you can also give an expression. For example, to double the score you could set the value of the variable score to value 2*score.
If a variable has a value. With this action you can check what the value of a particular variable is. If the value of the variable is equal to the number given, the question returns true and the next action or block of actions is performed. If it is not equal, the next action or block of actions is not performed. You can also indicate that the check should be whether the variable value is smaller than the given value or larger than the given value. Actually, you are not restricted to using variables but can check any expression you like. Later, we will see a number of examples of the use of these actions.
There is one more thing you need to know about variables. As I indicated above there are local variables that "belong" to an instance and there are global variables. When you use your own variables these are always local variables that only exist for the instance in whose actions you use them. If you want to use your own global variables you have to prefix their name with the word global and a dot. So, for example, you can use a variable global.bonus to indicate the number of bonus points gathered by the player. Always be careful that you use variable names that do not yet exist and are also different from the names of sprites, sounds, etc. One way to achieve this is to always let your variables start with e.g. var_.