Using Expressions And Variables In Actions

This section covers how to use expressions and the in-built instance variables in actions.

When using many of the actions in GameMaker: Studio you are expected to input a value as one of the parameters, but sometimes that is not enough and you may find it necessary to input a formula or an expression to get the required result from the action. this is perfectly valid and GameMaker: Studio permits the use of any of the in-built instance variables, constants or global variables as well as the use of your own (previously defined) variables and even mathematical formula! The image below shows how this can work: As mentioned before, GameMaker: Studio also has a great number of in-built variables that are unique to each and every instance created in a room. These instance variables are created for you automatically when you create an object and can be used, set and changed just as you would any other variable. Below is a list of all these variables:

  1. x: the x-coordinate of the instance in the room. Changing this will position the instance elsewhere in the room along the x-axis.

  2. y: the y-coordinate of the instance in the room. Changing this will position the instance elsewhere in the room along the y-axis.

  3. hspeed: the horizontal speed (in pixels per step) at which the instance is moving. A positive value means movement to the right and a negative value means movement to the left.

  4. vspeed: the vertical speed (in pixels per step) at which the instance is moving. A positive value means movement to the bottom and a negative value means movement to the top.

  5. direction: the current direction of motion in degrees (0-360), where 0° is right, 90° is up, 180° is left and 270° is down.

  6. speed: the current speed of motion in the current direction. For a "forward" motion (ie: facing the direction) this is a positive value, and for a "backwards" motion it should be negative.

  7. visible: this is the flag that sets an object as being visible (the draw event is performed) or invisible (the draw event is skipped).

  8. image_index: this variable indicates which subimage in the current sprite is currently shown. When the animation speed (image_speed, see below) is set to a value greater than 0, this variable changes each step to reflect the sub-image being shown of the animation .

  9. image_speed this variable indicates the speed with which the subimages are shown, with the default value being 1 (meaning 1 frame per step). If you make this value larger than 1 some subimages are skipped to make the animation faster, and conversely, if you make it lower than 1, then some sub-images are repeated over various steps to make it animate slower.

  10. id: the unique identifier (an integer) of the instance. This value is assigned to the instance when it is created and cannot be changed.

  11. object_index: this is the index (not the name) of the object that the instance has been created from. This value is assigned to the instance when it is created and cannot be changed.

Further information on in-built instance variables can be found in the following sections:

  1. Reference: Movement Variables
  2. Reference: Instance Variables
  3. Reference: Physics Variables

Apart from the in-built instance variables, we also have a number of global variables that can be used in actions too (see Variables And Variable Scope for more information on global variables). Here are a few of those available:

  1. score: the current value of the score
  2. lives: the current number of lives
  3. health: the current health (0-100)
  4. mouse_x: x-position of the mouse
  5. mouse_y: y-position of the mouse


Back: Actions
© Copyright YoYo Games Ltd. 2018 All Rights Reserved