Using expressions and variables

Game Maker 8

Using expressions and variables

In many actions you need to provide values for parameters. Rather than just typing a number, you can also type a formula, e.g. 32*12. But you can actually type much more complicated expressions. For example, if you want to double the horizontal speed, you could set it to 2*hspeed. Here hspeed is a variable indicating the current horizontal speed of the instance. There are a large number of other variables that you can use. Some of the most important ones are:

x the x-coordinate of the instance
y the y-coordinate of the instance
hspeed the horizontal speed (in pixels per step)
vspeed the vertical speed (in pixels per step)
direction the current direction of motion in degrees (0-360)
speed the current speed in this direction
visible whether the object is visible (1) or invisible (0)
image_index this variable indicate which subimage in the current sprite is currently shown. If you change it and set the speed to 0 (see below) you can display a fixed subimage.
image_speed this variable indicates the speed with which the subimages are shown. The default value is 1. If you make this value larger than 1 some subimages are skipped to make the animation faster. If you make it smaller than 1 the animation becomes slower by repeating subimages.
score the current value of the score
lives the current number of lives
health the current health (0-100)
mouse_x x-position of the mouse
mouse_y y-position of the mouse
You can change most of these variables using the set variable action. You can also define your own variables by setting them to a value. (Don't use relative, because they don't exist yet.) Then you can use these variables in expressions. Variables you create are local to the current instance. That is, each object has its own copy of them. To create a global variable, put the word global and a dot in front of it.

You can also refer to the values of variables for other objects by putting the object name and a dot in front of them. So for example, if you want a ball to move to the place where the coin is you can set the position to (coin.x , coin.y). In the case of a collision event you can refer to the x-coordinate of the other object as other.x. In conditional expressions you can use comparisons like < (smaller than), >, etc.

In your expressions you can also use functions. For example, the function random(10) gives a random integer number below 10. So you can set for example the speed or direction of motion to a random value. Many more functions exist. They are described in Part 4 of the documentation.