How do you use alarms?

Discussion in 'Programming' started by debleb, Jul 19, 2019.

  1. debleb

    debleb Member

    Joined:
    Jul 17, 2019
    Posts:
    10
    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?
     
  2. Arashi

    Arashi Member

    Joined:
    Feb 6, 2017
    Posts:
    138
    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();
    }
     
  3. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,452
    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: Jul 19, 2019
    Sozidar, Anixias, MissingNo. and 5 others like this.
  4. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    310
    Code:
    // text object create event
    alarm[0] = room_speed * 2; // this sets a 2 second alarm
    
    //alarm[0] event
    instance_destroy(();
    
     
    Stratadox and rIKmAN like this.
  5. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    310
    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.
     
    Mick, MissingNo. and rIKmAN like this.
  6. jackquake

    jackquake Member

    Joined:
    Aug 17, 2016
    Posts:
    81
  7. Arashi

    Arashi Member

    Joined:
    Feb 6, 2017
    Posts:
    138
    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: Jul 19, 2019
  8. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    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.
     
    Sabnock likes this.
  9. debleb

    debleb Member

    Joined:
    Jul 17, 2019
    Posts:
    10
    Thanks! I think I get how it works now.
     
  10. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,011
    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.
     
  11. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    310
    If you set the alarm to -1 it stops and doesn't run the code inside it.
     
  12. maru_th_undrtkr

    maru_th_undrtkr Member

    Joined:
    Jan 23, 2018
    Posts:
    45
    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
     
  13. Doulos

    Doulos Member

    Joined:
    Jun 8, 2019
    Posts:
    17
    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.)
     
    SnotWaffle Studios likes this.
  14. Geoff Jones

    Geoff Jones Member

    Joined:
    Aug 20, 2016
    Posts:
    499
    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.
     
  15. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    "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?
     
  16. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    @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.
     
    woods likes this.
  17. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,461
    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: Jul 20, 2019
    Anixias, Sabnock and RefresherTowel like this.
  18. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    see.... THIS is why i lurk around on these boards.... i learn stuff here... and not just fed information.. but "taught"


    thank you ;o)
     
    Cpaz and Sabnock like this.
  19. Doulos

    Doulos Member

    Joined:
    Jun 8, 2019
    Posts:
    17
    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.
     
  20. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,461
    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.
     
    Anixias, rIKmAN, Ladi_Pix3l and 2 others like this.
  21. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    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.
     
  22. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,791
    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.
     
    jackquake likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice