Custom Timer Question

Posted by LAR Games on June 25, 2013, 2:18 a.m.

Is it possible to use loops to make simpler custom timers?

I usually do something like this. (Variable names are for clarity.)

Quote: Create Event

// How long the timer will last.


//The countdown variable


Quote: Step Event

//Subtract 1 from the countdown every step


// If it reaches 0, reset it, and execute whatever you want to happen when it reaches 0

if hypotheticalTimerCountDown<=0{

hypoteticalTimerCountDown=hypotheticalTimerDuration //Reset part

(Insert other code here) //What you want to happen part



F1ak3r 11 years ago

No, a loop's not what you're looking for. If you run a repeat/for/while loop in the step event, it'll go through every iteration of the loop in a single step. What you're doing now is fine, but I'd personally just use GM's alarms – that way incrementation is handled automatically.

Quote: Create Event
alarm[0] = 20

Quote: Alarm 0
//code to do whatever

alarm[0] = 20 //reset alarm

Loops are good for saving yourself from copying lines of code with minor variations over and over again, the classic example being using a for loop to handle array assignment.

Instead of

arr[0] = 0


arr[9] = 9

you can just do

for (i = 0; i < 10; i += 1)


arr = i

where it first runs "i = 0", then checks if "i < 10", then if that's true, runs arr = i, then runs "i += 1", then checks if "i < 10" again, then if true runs "arr = i" again, and so on and so forth until "i < 10" is false, at which point it stops looping and goes on to the rest of your code.

Not sure if that makes sense – I remember the section on for loops in the GM helpfile baffling me for years before it finally clicked.

LAR Games 11 years ago

I used to use alarms all the time. Over time I realized I could do pretty much the same thing manually, with much more control over them and no cluttered event list in an object.

You definitely answered my question, though. Thanks.

Moikle 11 years ago

I don't like alarms. it is hard to remember which one is for what, and you have a limited number of them. I just create an ordinary variable, and deduct one from it at the end of every frame. if it gets into negative numbers, that is fine. you only need to be checking to see if it is equal to zero.

F1ak3r 11 years ago

True enough. But then, GM has a lot of things that are just numbered where names would be better: user events, arguments, alarms… I guess I just got used to it.

Acid 11 years ago

LAR did you get my message? Did it make sense?

LAR Games 11 years ago

Oh, yeah. I got it. Sorry for not replying!

I skimmed it a bit, but I haven't had a chance to study/ mess around with it yet. I'm sure it'll help.

Thanks, again!