How do you use alarms?

D

debleb

Guest
I'm working on my first game and when you inspect something, it should show text for a while and then delete it. I'm doing the text by creating a draw object that is created at the appropriate time, but I can't figure out how to delete it on a timer. I was thinking an alarm would work but I'm not entirely sure how to use them. Any help?
 

Arashi

Member
I do not. Alarms are just a weird timers, designed for drag-and-drop users. If you do not use drag and drop, you should not use alarms.

Code:
//create event:

timer = 0;
time = 120;

//step event:

timer++;
if timer>time
{
instance_destroy();
}
 

rIKmAN

Member
If you do not use drag and drop, you should not use alarms.
What's your reasoning for that?
An alarm is an event just like any of the others and is perfectly valid to use whether you use DnD or GML.

Your method is also very much valid and preferable to some people, but please don't spread nonsense like "alarms are only for DnD users" because it's simply not true.
 
Last edited:

Arashi

Member
Alarms are mega useful in GML can't think of a time when i haven't used them.

if people foolishly take this advice they are missing out on one of GMS's most powerful features.
I fail to see, what is useful or powerful in alarms?
Your code executes outside your (and everyone's) step event and it's execution can be unexpected. I find it very dangerous.

Like objects interracted with ech other inside step event, but one of the objects had allarm set, so after step event interraction something else happened to that object.

Example: player stunned a wolfbit for 1 second, than killed it. But allarm returned it to normal state from dead state. It will be a rare and hard to find bug.
 
Last edited:
A

Annoyed Grunt

Guest
I fail to see, what is useful or powerful in alarms?
Your code executes outside your (and everyone's) step event and it's execution can be unexpected. I find it very dangerous.

Like objects interracted with ech other inside step event, but one of the objects had allarm set, so after step event interraction something else happened to that object.

Example: player stunned a wolfbit for 1 second, than killed it. But allarm returned it to normal state from dead state. It will be a rare and hard to find bug.
Deferred code execution is a thing anywhere and if it causes problems it doesn't mean it doesn't work, it means you're not coding it right.
 

Toque

Member
There are a few tutorials on youtube .

This guys is pretty good. There are others too.

I admit I dont always use them. But I just used one today so I find good use for them.
 

Sabnock

Member
I fail to see, what is useful or powerful in alarms?
Your code executes outside your (and everyone's) step event and it's execution can be unexpected. I find it very dangerous.

Like objects interracted with ech other inside step event, but one of the objects had allarm set, so after step event interraction something else happened to that object.

Example: player stunned a wolfbit for 1 second, than killed it. But allarm returned it to normal state from dead state. It will be a rare and hard to find bug.
If you set the alarm to -1 it stops and doesn't run the code inside it.
 
M

maru_th_undrtkr

Guest
The only argument i heard against alarms is that its linked to room speed, but i think optimizing anything is bad until ur able to do it. And then its still not always worth it
 
D

Doulos

Guest
I use alarms all the time in drag and drop. works great.
I suggest 1st, you make a name for every alarm instead of setting alarm 0 or 1, etc. You make a variable called (for example) AlarmDestory; then set that equal to 0.
Then you call your AlarmDestory with the countdown to how long you want that object to stick around.
Which I would also suggest that you use variables for the duration.
Thus 2ndly, you make a variable called (for example) AlarmCountdown; then set that equal to 120 (assuming 60 frames that would be 2 seconds.)
 

Geoff Jones

Member
I don't use them myself. I've always found them to be a bit messy. Instead, I use a simple script that returns true if the timer has counted down.
If you are new to GM, I would suggest learning alarms though.
 

woods

Member
"The only argument i heard against alarms is that its linked to room speed,"

so is the step event..
as i understand it, step event run roomspeed times in a second, pretty much the same as an alarm counts down... wether you create a fro loop that counts off inside your code, or set alarm[0] to count of and call back to the code...

they do basically the same thing right?
 
@woods No, a for loop is not like an alarm at all. They are the exact opposite. A for loop will always execute it's entire range from 0 to n in a single step, whereas alarms are specifically designed to take n-steps until they execute. What maru was talking about is that you have the option of increasing or decreasing the amount of time a countdown timer will "move" in the step event, whereas you don't in the alarm event.

For instance:
Step Event code:
Code:
timer += 2;
if (timer > room_speed) {
   // Do thing
   timer = 0;
}
This codeblock will be activated every room_speed/2 steps, despite the fact that it's trigger is actually room_speed. You could add a variable, like timer += timer_step, and then you have a timer that you can directly control how fast or slow it counts down, without having to change the point at which it triggers. You can't do this with alarms. How useful it is, is up to you to decide, but that's the general point.
 

FrostyCat

Redemption Seeker
You could add a variable, like timer += timer_step, and then you have a timer that you can directly control how fast or slow it counts down, without having to change the point at which it triggers. You can't do this with alarms. How useful it is, is up to you to decide, but that's the general point.
This is a false choice that a lot of GML users assert about alarms. If you fake the underlying framework and use event_perform(), it's perfectly possible to do what you did manually with a reusable solution based on alarm events.

Consider this:
Code:
///@func has_alarms([n])
var n = (argument_count == 0) ? 12 : argument[0];
timer = array_create(n, -1);
timer_speed = array_create(n, 1);
Code:
///@func run_alarms()
for (var i = array_length_1d(timer)-1; i >= 0; i--) {
  var t = timer[i];
  if (t >= 0) {
    t -= timer_speed[i];
    if (t < 0) {
      event_perform(ev_alarm, i);
      t = -1;
    }
    timer[i] = t;
  }
}
Now if you run has_alarms(); in the Create event and run_alarms(); somewhere in the Step event, you have the same ordering, timer speed and fine-grain control as custom variables but also access to built-in Alarm events. You can now inherit individual Alarm events, where you can't with the equivalent step check. Furthermore, the same framework can be used with pausing, delta time or any other adjustments that differ from the standard one-tick-per-step assumption.

Edit: Default should have been 12 for alarms 0 through 11.
 
Last edited:
D

Doulos

Guest
you can change countdowns alarms while it is counting. At any time you want you can add to, subtract from, reset or cancel an alarm.
Just use get alarm then Set alarm (both available in dnd)
-1 countdown turns them off.

The only thing you cannot do (at least I have nto seen it) is ask to do the alarm "right now". Shortest is 1, which activates next frame.
If you want the Right now part, you can make the alarm call an event. Then if you need "right now" you set the alarm to -1 and call the event directly.
 

FrostyCat

Redemption Seeker
The only thing you cannot do (at least I have nto seen it) is ask to do the alarm "right now". Shortest is 1, which activates next frame.
If you want the Right now part, you can make the alarm call an event. Then if you need "right now" you set the alarm to -1 and call the event directly.
You can make an alarm event trigger right away if you use event_perform(ev_alarm, n).

It's clearly evident from this topic how the reputation of events outside the most basic 6 (Create, Step, Draw, Draw GUI, Destroy, Cleanup) is more driven by inexperience, speculation and fake news than practical experience with them.
 

woods

Member
driven by inexperience, speculation

which is why most of my responses are in question form ;o)
if i can point in a POSSIBLE direction, or spark a thought process that was missed or forgotten... there is potential for forward motion.

at least i know enough to know that i dont know what im talking about... (meaning i am definitely NOT the goto guy) but im not afraid to take a slightly uneducated guess.
 

TheouAegis

Member
And the alarm variables themselves do nothing without an alarm event, so you could just use them in your own timer code.

Code:
alarm[0] = 30;
Code:
if !alarm[0]-- {
   ...
}
In one project i used alarm events as usual. In another project I did not want the alarms to count down at the start of a step or even count down together, so I manually decremented them. Do what works for your project.
 
Top