Alarm

An Alarm is a kind of timer that triggers something else to happen.

Alarms are useful for when you want to count down time. These include simple countdown timers, a time limit for the player to achieve a goal, and a waiting time or delay between things happing in your game.

How Alarms Work

  • A "count down" time is stored in a variable.
  • Each game loop, 1 is subtracted from the alarm variable.
  • When the alarm reaches 0, something is triggered and the alarm is set to -1 to shut itself off.

Example Code

This example code is a complete example for a simple alarm in use.

Setting the Alarm

The first step in using an alarm is to give it the amount of time to count down. This activates the alarm and it begins counting down immediately.

The alarm should be set in number of game loops as a unit of time.

You can tell how many game loops there should be in a second by noting what you set the FPS to using SetFPS. The example code sets it to 60 frames per second.

Then just multiply the number of seconds you want to set the alarm to by the number of game loops.

From the example code above :

alarm = 60 * 5

Counting Down and Triggering the Event

The alarm counts down using this If/ElseIf statement :

If alarm > 0
        alarm = alarm - 1
    ElseIf alarm = 0
        boom = True
        alarm = -1
    EndIf

This will only subtract one from the alarm variable if it is above 0.

When the alarm reaches 0, it sets boom (the event flag) to True and sets the alarm variable to -1, which effectively shuts off the alarm and keeps it from triggering the event over and over and over.

Related Pages

Categories: Timing