In GameMaker, timers are used to execute code after a certain amount of time has passed. A timer can be implemented using alarms or by manually tracking the passage of time using variables.
GameMaker provides a built-in feature called "alarms." Each instance has twelve alarms (from
alarm) that can be set to go off after a certain number of steps (frames).
Here's how you set an alarm:
And this is how you might respond to an alarm:
Sometimes you might need more flexibility or additional timers beyond the 12 provided. For that, you can create your own timer using variables and step events.
Here's how to implement a custom timer:
Remember to manage the state of
timer_active as needed to start or stop the timer.
For precise timing that takes into account potential variations in frame rate, you can use delta timing. GameMaker has the built-in variable
delta_time, which represents the time since the last step event in microseconds.
Here's an example of using
delta_time for a timer:
Timers are essential for everything from delays between actions, cooldowns on abilities, to animations and transitions. Whether using alarms or creating manual timers, both methods require you to keep track of the passage of time within your game's events and respond accordingly when the time is right.
Alarms are straightforward and integrated into GameMaker but offer limited slots, while custom timers require more coding but give unlimited flexibility. The choice depends on your needs and the complexity of your game.