If I want to setup multiple timers, how do I differentiate them inside the ALLEGRO_EVENT_TIMER event?
Thank you
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.
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.
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).
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();
I'd put the respawn time in the array of structs for the ships.
I haven't thought this through more than 2 minutes, so YMMV.
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
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); } }