Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multiple Timers

This thread is locked; no one can reply to it. rss feed Print
Multiple Timers
InfiniteLoop
Member #8,434
March 2007

If I want to setup multiple timers, how do I differentiate them inside the ALLEGRO_EVENT_TIMER event?

Thank you

Todd Cope
Member #998
November 2000
avatar

event.timer.source is a pointer to the timer that triggered the event. Just compare your timer pointers to this pointer to see which one triggered the event.

InfiniteLoop
Member #8,434
March 2007

You sir, are a gentlemen and a scholar.

How would you recommend setting the timer up so you can fire it off so that it comes back only once? For example, if a ship is destroyed, I want it to re-appear 2 seconds later. Obviously, I don't need the timer firing off every 2 seconds, only upon a ship destroyed.

Todd Cope
Member #998
November 2000
avatar

I typically use a single timer for my entire game running at 60hz. I run the game logic once for each timer event. If I wanted something to happen after two seconds I would store a countdown in a variable (starting at 120 in my 60hz setup to get 2 seconds) and decrement it each time the logic runs. When the countdown reaches 0, I do whatever it is I wanted to do.

if(shipdestroyed)
{
    countdown = 120;
}
if(countdown > 0)
{
    countdown--;
    if(countdown == 0)
    {
        respawnship();
    }
}

If you really wanted to use a separate timer to do this (I don't recommend this method), you can create the timer and use al_set_timer_speed(timer, 2.0) and al_start_timer(timer) when the ship is destroyed. When the timer event is triggered you can respawn your ship and use al_stop_timer(timer).

SiegeLord
Member #7,827
October 2006
avatar

I prefer an alternative approach to countdowns. I keep a global time variable (incremented every game logic step), this way I can specify when to do some action.

if(shipdestroyed)
    time_to_respawn = game_time + 120;
if(game_time >= time_to_respawn)
    respawnship();

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'd put the respawn time in the array of structs for the ships.

#SelectExpand
1struct _ship 2{ 3 int dead; 4 float x; 5 float y; 6 float angle; 7 float time_to_respawn; 8} 9 10 for(i=0;i<NUMSHIPS;i++) 11 { 12 if(ship[i].time_to_respawn > 0.0) 13 { 14 ship[i].dead = TRUE; //this might be done by bullet collision logic 15 ship[i].time_to_respawn -= timer_interval; 16 } 17 else 18 { 19 ship[i].dead = FALSE; 20 respawn_ship(&ship[i]); 21 } 22 23 . 24 . 25 . 26 }

I haven't thought this through more than 2 minutes, so YMMV.

They all watch too much MSNBC... they get ideas.

InfiniteLoop
Member #8,434
March 2007

Hey, thanks for the responses. I will agree that keeping a running time is a good idea, but I am still curious about firing off situational timers. Is that something that is possible? Even if it is not an optimal solution, I would still like to know how to do it if it is possible. Thanks

Todd Cope
Member #998
November 2000
avatar

Todd Cope said:

If you really wanted to use a separate timer to do this, you can create the timer and use al_set_timer_speed(timer, 2.0) and al_start_timer(timer) when the ship is destroyed. When the timer event is triggered you can respawn your ship and use al_stop_timer(timer).

Here is an example:

if(ship_hit)
{
    destroy_ship();
    al_set_timer_speed(mytimer, 2.0);
    al_start_timer(mytimer);
}

When you process events:

if(event.type == ALLEGRO_EVENT_TIMER)
{
    if(event.timer.source == mytimer)
    {
        respawn_ship();
        al_stop_timer(mytimer);
    }
}

Go to: