SOLVED How to create and run an alarm through code

EvanSki

Raccoon Jam Host
What I mean is

lets say I have an object EX: Cat
obj_cat has no alarm event, but calls a script
in that script I want obj_cat to do an alarm event, set how long the alarm is and what to do when the alarm hits 0

Any possible way to do this?
thanks
 
Last edited:

Nidoking

Member
I think your best bet is to set some variable, decrement it in the step event, and when it hits zero, run a script. I don't think you can create or alter events at runtime.

Well, I say best bet, but that's your second best bet in my opinion. Your actual best bet is to give the object an alarm event.
 

Freddy Jones

Your Main Detective
Dynamic functionality and call deferment are kind of a sore spot in gml.

You can create a generic alarm object yourself that you instantiate providing it a variable for the target and the script to run. You also have to define the script of code it expects to perform with the instance of cat that created it. Then you'd probably want to make sure that alarm instance gets destroyed.

You could also use time lines but they're technically even more limiting for your usecase than alarms alone.


If you wanted to go further for a generic step based alarm system without creating an instance every time you could create an alarm controller object that contains a list of targets, scripts, and alarm times where it will run through those every step and simulate what I described above.

Not sure what you should do after that
 
Assuming you're not using delta time,

Create:
GML:
timer = -1;
timer_script = -1; // Which script to run
Timer start script:
GML:
///@func timer_start(steps, script)
///@arg steps
///@arg script
timer = argument0;
timer_script = argument1;
Step:
GML:
if (timer > -1 && --timer == 0) {
    timer = -1;
    script_execute(timer_script);
}
 
Last edited:

kburkhart84

Firehammer Games
I have an easy way to kind of do what you want, but you would have to put any code you want in alarms into scripts/functions. You could actually create a generic object that has a simple alarm event, and then put a script_execute(scripttoexecute) call in the alarm, along with destroying itself afterwards. Then, your object creates an instance of the object using either obj = instance_create(xxxx) or with(instance_create(xxxx)){}, then you can use either the dot syntax or put within the brackets of the with{} the alarm[0] = somestepamount, and put scripttoexecute = somescript.....then the other object gets released to handle the alarm and execute whatever script it is that you set for it.

It isn't the best way to do it, but you could wrap the instance creation in your own script with a couple of arguments and that would make it a one-liner for whenever you needed some delayed code.

That said, this method once implemented is going to make one-liners everywhere instead of counting down variables...but you do have to implement it first(may take 10 minutes tops I would think). It does also increase a little overhead with having the objects out there...and you wouldn't be able to easily change those alarms or stop them without some additional workings, so simply doing a countdown variable in your step event could be the best way to go if any of that is an issue.
 
Top