Game Maker Tutorial

Page 2 of 13
Variables
Before we will actually create the scrolling shooter game we need to delve a bit into an important concept of Game Maker: the use of variables. This simple concept will provide a very powerful mechanism to enable much more interesting game play. So what is a variable? It can best be seen as a property of an instance of an object.
As you should know, there are certain properties we can indicate when we define an object. For example we can set whether it is visible or whether it is solid. There are also a number of actions that change certain properties. For example there is an action to change the position or the speed of the instance.
Each instance has a number of such properties and there are also a number of global properties, like the score, that are not related to individual instances. All properties are stored in so-called variables that have a name. Here are some of the properties/variables that each instance has:
x: the x coordinate of the instance
y: the y coordinate of the instance
hspeed: horizontal speed (in pixels per step)
vspeed: vertical speed (in pixels per step)
direction: current direction of motion in degrees, 0 being right, and going anticlockwise to 360 being right again
speed: the current speed in this direction
visible: whether the object is visible (1, or true) or invisible (0, or false)
solid: whether the object is solid (1, or true) or not (0, or false)
And here are some global variables:
score: the current value of the score
lives: the current number of lives
health: the current health of the player (defaults to 100)
mouse_x: the x coordinate of the mouse
mouse_y: the y coordinate of the mouse
room_speed: current speed (FPS) of the room in steps per second
room_caption: caption shown in the window title
room_width: width of the room in pixels
room_height: height of the room in pixels
There are many, many more variables, both local for the instances, and global. All of them can be found in the Game Maker documentation. There are actions which manipulate the value of certain variables but, as we will see, you can also manipulate them directly. What is better, you can define your own variables and use these as well.
For example, as we will see below, we want our spaceship to be able to shoot only once every five steps in the game. So our space ship needs a property to indicate whether it can shoot. We use a variable for this property that we call can_shoot. (A variable name can only consist of letters and the underscore symbol. Variable name are case-sensitive, so Can_Shoot is not the same variable as can_shoot.) In the creation event of the spaceship we set this variable to 1 (we always use 1 to indicate true). When the player wants to fire a shot we check the value of the variable to see whether this is allowed. Whenever a shot is fired we set the property to 0 (indicating that shooting is temporarily not allowed).
Next we use an alarm event to set the property back to 1 after five steps. Below we will describe this in more detail. In a similar way we can use variables to indicate whether the spaceship has an active shield, whether it has some special weapon upgrade, etc.