Skip to content

Alarms

Introduction to Alarms

In game development, timing is crucial for creating engaging and dynamic gameplay. GameMaker Studio provides a powerful feature called “Alarms” to handle timed events. Alarms can be set to trigger actions after a specific duration, allowing developers to schedule tasks within the game. This chapter will guide you through the basics of using Alarms in GameMaker Studio with GML Visual and demonstrate their practical application by moving a ghost in Pac-Man in a random direction and changing its color after Pac-Man collects a power-up.

What is an Alarm?

An Alarm is a built-in timer in GameMaker Studio that counts down to zero from a specified value. When the timer reaches zero, an event is triggered. Alarms are incredibly useful for managing timed actions, such as spawning enemies, creating power-ups, or controlling animations.

Key Features of Alarms

  1. Countdown Timer: Alarms work by counting down from a set number of steps (frames). Each step represents one frame of the game, so an alarm set for 60 steps will trigger after one second in a game running at 60 frames per second.
  2. Triggering Events: When an Alarm reaches zero, it triggers an event. This event can contain any actions you want to occur at that time, such as changing an object’s properties, creating new objects, or modifying the game environment.
  3. Multiple Alarms: Each object can have multiple Alarms (up to 12), identified by their index (from 0 to 11). This allows you to manage various timed events independently within a single object.
  4. Resetting Alarms: Alarms can be reset or adjusted at any time, allowing for flexible and dynamic game mechanics. For example, an Alarm can be reset to create a repeating action, such as a ghost changing direction every few seconds.

When to Set Alarms vs. When to Trigger Alarms

Understanding the appropriate situations for setting and triggering alarms is crucial for efficient game design.

  • Setting Alarms: Alarms should be set when you want to start a countdown for a timed action. This is typically done in events that initiate a sequence, such as the Create Event of an object, collision events, or any user-defined events that require a delay.
    • Example: Set an Alarm in the Create Event to make an object move after a delay or set an Alarm when a player collects a power-up to change the object’s state after a certain period.
  • Triggering Alarms: Alarms are triggered automatically when their countdown reaches zero. You define what happens when the Alarm triggers by adding actions to the Alarm Event. The triggering of alarms is managed by the game engine based on the countdown you set.
    • Example: When the countdown reaches zero, the Alarm triggers an event to move the ghost in a new direction or change its color.

Practical Uses of Alarms

  • Enemy Behavior: Control the movement patterns and behaviors of enemies. For example, make an enemy change direction or attack at regular intervals.
  • Power-Ups: Manage the duration and effects of power-ups. For example, change the player’s abilities for a limited time after collecting a power-up.
  • Animations: Synchronize animations or create delays between frames to achieve smooth transitions and effects.
  • Timed Events: Trigger events like spawning new objects, initiating cutscenes, or changing levels after a certain period.

Example of Using Alarms

Let’s create an Alarm that moves a ghost in Pac-Man in a random direction once it’s been created.

  1. Create the Ghost Object:

    • Open your project in GameMaker Studio.
    • Create a new object and name it obj_ghost.
    • Add a sprite to represent the ghost.
  2. Add the Create Event:

    • In the obj_ghost object, add a Create Event.
    • In the Create Event, add the following GML Visual actions:
      • Set Alarm [0] to 60 steps (1 second).
  3. Add the Alarm [0] Event:

    • In the obj_ghost object, add an Alarm [0] Event.
    • In the Alarm [0] Event, add the following GML Visual actions:
      • Generate a random direction.
      • Move the ghost in that direction.
      • Reset the Alarm [0] to trigger again after 60 steps.

Step-by-Step Guide

  1. Create Event:

    • Open the Create Event of obj_ghost.
    • Add the action to set Alarm [0] to 60 steps.
      • Drag the “Set Alarm” action to the workspace.
      • Set the Alarm number to 0 and the number of steps to 60.
  2. Alarm [0] Event:

    • Open the Alarm [0] Event of obj_ghost.
    • Add the actions to move the ghost in a random direction.
      • Drag the “Move Fixed” action to the workspace.
      • In the “Direction” parameter, choose random directions (up, down, left, right).
      • Set the speed to the desired value (e.g., 4).
    • Reset the Alarm [0] to 60 steps.
      • Drag the “Set Alarm” action to the workspace.
      • Set the Alarm number to 0 and the number of steps to 60.

Example Code in GML Visual

  1. Create Event Actions:

    • Set Alarm [0] to 60 steps.
  2. Alarm [0] Event Actions:

    • Choose random direction:
      • Drag the “If Variable” action to check a random number.
      • Set conditions to choose between 0, 1, 2, and 3.
      • Assign direction based on the random number.
    • Move ghost in the chosen direction.
    • Reset Alarm [0] to 60 steps.

Practical Example: Changing Ghost Color After Power-Up Collection

In addition to moving the ghost in a random direction, we can use Alarms to change the ghost’s color after Pac-Man collects a power-up.

Step-by-Step Guide

  1. Create the Power-Up Object:

    • Create a new object and name it obj_powerup.
    • Add a sprite to represent the power-up.
  2. Add Collision Event with Pac-Man:

    • In the obj_powerup object, add a Collision Event with the Pac-Man object (e.g., obj_pacman).
    • In the Collision Event, add the following GML Visual actions:
      • Destroy the power-up object.
      • Set Alarm [1] of the ghost object to 120 steps (2 seconds).
  3. Add Alarm [1] Event to Ghost Object:

    • In the obj_ghost object, add an Alarm [1] Event.
    • In the Alarm [1] Event, add the following GML Visual actions:
      • Change the ghost’s color.
      • Reset the color after a certain duration.

Example Code in GML Visual

  1. Power-Up Collision Event Actions:

    • Destroy the power-up object.
    • Set Alarm [1] of obj_ghost to 120 steps.
  2. Ghost Alarm [1] Event Actions:

    • Change ghost color:
      • Drag the “Change Color” action to the workspace.
      • Set the desired color (e.g., blue).
    • Reset color after a certain duration:
      • Set Alarm [2] to 180 steps (3 seconds).
  3. Ghost Alarm [2] Event Actions:

    • Reset ghost color:
      • Drag the “Change Color” action to the workspace.
      • Set the original color.

Conclusion

Alarms are an essential tool in GameMaker Studio for managing timed events in your game. By understanding how to set and use Alarms, you can create more dynamic and engaging gameplay experiences. The examples of moving a ghost in Pac-Man and changing its color after a power-up demonstrate practical applications of Alarms, showcasing their ability to control game mechanics effectively. Experiment with Alarms in your projects to unlock their full potential.