GM: Alarms
From GMpedia.org Wiki
|
This tutorial works with... |
|
All versions of Game Maker. |
Alarms are something nearly every good game needs, anything that needs timing, needs an alarm (you can use timelines and these are discussed in a future Timeline Tutorial). You may find the concept of alarms confusing to grasp at first, but once you understand, you'll wonder how you ever did without them!
Contents |
[edit] The countdown begins!
If you think of an alarm as a countdown timer, you're half way to understanding them, once an alarm is called from an event, the countdown begins! Take the following call to an alarm for example:
alarm[0] = 90;
What happens when the game comes across an alarm call, like the one above, is a countdown begins from the number after the equals sign, when it reaches 0, the specified alarm event is called. (There are 12 alarm events that can be called per object, these are numbered [0] - [11]).
As mentioned, the countdown starts from the number after the equals sign, in the above example, 90, but what exactly does that number represent? You may think it's the number of seconds to countdown before calling the alarm event, but it isn't, it's the number of STEPS.
[edit] STEPS?
- For more information about the concept of steps in Game Maker, see GM: Step.
When your game runs, it runs in STEPS (or sometimes known as frames). The default for a Game Maker game is 30 steps per second (see GM: Room speed) and can be found in the Room Properties. (Each room can have a different speed if need be):
To calculate the number of steps in the number of seconds you want, you must do the following:
Room Speed x Number of Seconds
Therefore, taking the number of steps per second into account, the 90 in the above example translates to 3 seconds, like so:
3 x 30 = 90
[edit] How do you use them?
Hopefully, you have grasped the basic concept of alarms but how do you actually use them? Well, one example would be in a platform game where a cannon, on one side of the screen, fires a cannonball across to the other side after 5 seconds, but how?
In the Create Event for the cannon object, you could have something like:
And here's the GML version:
global.cannonshots = 10; cannonangle = 270; alarm[0] = 150
So as soon as the cannon object is created, it will do the various things then hit the alarm event: Set Alarm 0 to 150, starting the countdown in steps. [Remember it's SECONDS x ROOM SPEED: 5 x 30 = 150].
You would also create an alarm[0] event and place the things in there you wish to happen when it's called, in this case, after 5 seconds:
You'll notice in the alarm[0] event above, there is another alarm call at the end, and this alarm call is calling itself! This is useful for looping alarms and in the above example, a cannonball will shoot across the screen every 5 seconds as the alarm event is resetting the countdown to recall itself again.
Here's the GML version:
// Create the cannonball and move it . . . // Call this alarm repeatedly alarm[0] = 150
[edit] See Also
See the talk page for details




