Game Maker Programming/Variables

Variables hold information. These bits of information are given names so they can be identified, and changed more easily. For example, if a = 4, and b = 7, a+b would equal 11.

In Game Maker, variable names can only have the following kinds of characters:

  • Letters
    • Lowercase and Uppercase
    • Variables are case-sensitive, for instance: PenColor is NOT the same as pencolor
  • Digits
    • A variable name cannot start with a digit.
  • The underscore symbol "_"

In Game Maker, variables are defined by the name of the variable, an equals sign, and the value of this variable. For example:

 if (Percent>66)
 else if (Percent>33) 

A variable can also be declared by using var, for example, var variable1. Using var to declare a variable only makes that variables available during that piece of code - it is removed from memory after use. This makes your game use less memory, and can be very efficient. However, it is not required.

By default, Game Maker reports an error if you try to use an undefined variable before assigning to it. If you receive the error, you should initialize it in the create section with the default value. An option in Global Game Settings allows you to treat uninitialized values as 0, as well as to disable the display of error messages; however, caution should be used when using these two options since it will make bugs more obscure.