Variables

Variables are absolutely essential if you want to make a GameMaker game at anything other than novice level. Each variable has a value which can be edited, taken and used, drawn in text, and much more. A variable, therefore, is a slot in the memory of the computer that can be used to store data for later use.

List of some preset variables

x
y
hspeed
vspeed
direction
speed
visible
image_index
image_speed
score
lives
health
mouse_x
mouse_y
Custom

An introduction to variable types

Instance Variables
Instance variables are those variables which can be used only within the scope of an instance who has declared them. These are the most common type of variables. These are generally declared in the Create event:

apples = 5;
red = $FFFFFF;
hate = "love";

Now these variables can be used only within the object/instance you have declared with. Not elsewhere.

Global Variables
Global variables allow any variable to be used anywhere in the game. All you have to do to make a variable global is just add a "global." prefix before declaring the variable:

global.break = 5;
global.nose = "hat";
global.five = 50 * global.break;

Local Variables
These are just variables used within the certain code, and isn't to be used anywhere else. You would use the keyword "var" in order to declare a local variable. If you want to declare multiple local variables, separate them with commas. Also, you must have the semi-colon at the end of the last variable:

var xx, yy, i;

From this, you don't have to worry about unwanted oppositions between two of the same variables. But beware! You can use these variables only where you declared them. If you declared them in the Step event, they can't be used in the Draw event, that is, they terminate at the end of the event/script they are declared with.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License