• Home
  • Tutorials
  • Downloads
  • Games
  • Articles
  • Contact

Variables

A variable is an information store that contains a value, which can be accessed by name. For example, an object might have a variable called hit_points set to 100 which defines whether the object is still "alive". Being able to check that value, and to change it, is vital.


You will frequently create your own variables, but Game Maker also features a variety of predefined variables. For example, referring to the default global variable mouse_x will retrieve the x coordinate of the mouse. Referring to the x variable of an object instance will retrieve the individual x position of that object instance.

There are 2 types of variables, local and global. These are known as the "scope" of a variable.

Variables must be declared before they can be manipulated. It is good practice to declare your variables in the Create event of an object, though they can be declared in any event.

If you wanted each instance of an object to have its own mana_points variable, in the create event for the object you would place:
Picture
Each instance of that object would then have it's own mana_points local variable once created.  You can then manipulate that variable, but as it is local it can only be manipulated from within the instance itself.
Global variables can be accessed from any object. For example, say you wanted to have a global variable which stored the number of lives a player had left:
Picture
The addition of global. at the front of the variable name declares this as a global variable.

When declaring variables, note that they must not start with a number, and can contain only letters, numbers, and the underscore symbol.

Now that you know what variables are, the next step is learning how to manipulate them.
Picture
Picture

How To Make An MMORPG © 2010-2012
Copyright | Privacy Policy