Improving a Timer System

11clock

Member
I have a custom timer system going on.



It is decent, but I am seeing an obvious problem with it. Notice that I use the same pattern for each cooldown variable, and I need to type each cooldown variable 3 times. How could I improve upon this to reduce repetitive code?
 

Tthecreator

Your Creator!
This is not as problematic as you think.
You could setup some weird system with a data structure that holds data and uses id's for your timers and stuff.
And although you could do it in very reasonable time, it's still not that much worth it to do.
 

11clock

Member
This is not as problematic as you think.
You could setup some weird system with a data structure that holds data and uses id's for your timers and stuff.
And although you could do it in very reasonable time, it's still not that much worth it to do.
Well now I have quite a few cooldowns and having to type each cooldown variable 3 times is getting on my nerves.

Still looking for a better way to do this.
 
J

JackOatley

Guest
GM does support pointers with arrays. So if your cooldown variable held an array (even if it only had the 1 index), you could pass that into the timer_update function and not need to pass it back out, but change it directly like so:
Code:
var array = argument0;
array[@ 0] = array[0] + delta;
Which would change your "var = func(var)" to just func(var).

What's in timer_update? You're only passing it the same number you want back...?
I usually do something like:
Code:
if ( myTimer-- <= 0 ) {
    // code
    // myTimer = cooldown;
}
 

AllCrimes

Member
I personally use a grid with three columns. The first column is "Name", the second is "Time Remaining", the third is called "Reply" which contains a script to run on expiration of the cooldown or -1 for no script. Any object with a cooldown grid runs a script called manage_cooldowns in their step event. The manage_cooldowns script cycles through the Y axis of the name column, and adjusts the time remaining column by -1 for each valid entry it finds. If a time remaining column reaches 0 the reply script is executed if there is one. Afterwards, the entire row is deleted for reuse.

When I want to add a cooldown, I use a script create_cooldown(obj_id,"name_of_cooldown",time,reply_script). If there is not enough space in the grid it resizes the grid by making it taller.

I also have another script, called on_cooldown(obj_id,"name_of_cooldown") which uses ds_grid_value_exists to check if a certain cooldown exists in the name column.

So essentially with this handy scripts I could use the following logic...

Code:
if !on_cooldown(player_object,"Shoot")
{
create_cooldown(player_object,"Shoot",500,-1)
}
 
Top