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:
- Lowercase and Uppercase
- Variables are case-sensitive, for instance: PenColor is NOT the same as pencolor
- 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:
Percent=(Health/MaxHealth)*100; PenColor=c_black; if (Percent>66) brush_color=c_green; else if (Percent>33) brush_color=c_yellow; else brush_color=c_red; draw_rectangle(x+((sprite_width/2)-25),y-5,x+((sprite_width/2)-25)+(50*(Percent/100)),y-1); draw_sprite(sprite_index,-1,x,y); score=Percent;
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.