GMS 2 Call an Alarm event on an instance id

Discussion in 'Programming' started by Heath Close, Aug 6, 2017.

  1. Heath Close

    Heath Close Guest

    I have a drag and drop on an object that is set up to shake it. I want to be able to call it from another object so I put it in an alarm, one that is never used so I figured it would be safe to put it there.

    I want to call this alarm event from another object... how do I get this code to work?

    Code:
    event_perform_object(inst_1EC08766,ev_alarm,0);
     
  2. 2Create

    2Create Member

    Joined:
    Jun 20, 2016
    Posts:
    17
    It sort of depends on whether you want to call the alarm event in all instances of an object, or only in one specific instance.

    Each instance has an id that is assigned it runtime, so if you want to call it in one instance, you will need to find a way to pass that id on to the instances that need to call it. This could for example be done by setting a global variable to that instance's id, and then using that variable to call the alarm event. In code, that would look like this:
    In the object that needs its alarm event called (after ascertaining it's the right instance)
    Code:
    globalvar instanceID = id;
    And then in the object that needs to call it:
    Code:
    event_perform_object(instanceID,ev_alarm,0);
    If you're placing the instance in the room editor, the former code snippet could also go in its "creation code".

    If all instances need to be affected, just do it like this (replace objectName with the name of the object):
    Code:
    event_perform_object(objectName,ev_alarm,0);
    The 0 at the end is the number of the alarm. So if you use alarm 0 it remains the same, but if you use alarm 6 you'd set it to 6.

    I hope this helps!
     
    Heath Close likes this.
  3. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,901
    You can't use an instance id, it has to be an object ID...

    Personally, I'd use a User Event rather than an alarm then you can do:

    Code:
    with (OBJECT or INSTANCE)
    {
    event_user(0);
    }
    This is much cleaner and pretty much the kind of thing that User Events were designed for. ;)
     
    Heath Close and 2Create like this.
  4. Heath Close

    Heath Close Guest

    Thank you both very much.... I will try both ways... but I would need to look into how to make a user event... I'll tackle it in the morning, thank you
     

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