Time based event handling

Go To StackoverFlow.com


I am currently working on writing a simple game in order to learn how to use SFML in C++. So far things have been going smoothly and I have a basic understanding of most things to do with SFML. The problem I have run into is finding an efficient way to and time based events.

The game I'm working on is a very simple space invaders esque game that has waves of enemies come in at predefined times during the level. As of now I have an event class that holds and controls what is supposed to happen with each called event in order to allow me to reuse simple events multiple times. As of now I trigger these events by looping utilizing an SFML cloock and with each iteration of the game loop running through a vector of all of the events for the current level and comparing the elapsed time on the clock with the specified time for the event to be called. The problem is that I have to check an entire vector of events every game loop iteration and if the event list get long enough I am worried it will begin to have an impact on the performance of the game.

I had an idea to give each event a simple numerical Id for its position in the timeline and simply store which event is supposed to run next that way I would only have to check the time of one event per loop iteration, and while this will work fairly well I think, I was curious if a more efficient method that didn't require a check every loop iteration was possible? I looked a bit into event driven programming but could not find much specifically related to time based events.

Thus I was wondering if anyone has had any experience with timelines or time based event triggering and would have any tips or resources that I could look at to figure out a more efficient idea? Any help would be great, thanks!

2012-04-04 17:02
by Zack
Time an event is supposed to run at is its ID, there's no need for another number (unless you need it for other purposes) - n.m. 2012-04-04 17:06
Keep the event list ordered by time so you only need to look at the first entries - Hans Passant 2012-04-04 17:15


The method you suggest is just one comparison each time through the game loop. It doesn't get any faster than that.

To allow multiple events to fire at the same instant, use a multimap, where keys are event times, and values are the events themselves. The multimap will then be sorted by time.

Each time through the game loop, do something like this (pseudocode):

now = getCurrentTime()
while not events.isEmpty() and events.firstElement().key() < now:
  e = events.firstElement().value
2012-04-04 17:18
by Thomas


Sort your vector based on the events' times and then just store how far you've gotten through the vector so far. Each loop then you'll advance that position until the next event shouldn't occur yet, and fire off the events you just iterated over.

std::vector<Event> time_line;
size_t time_line_position;

void fire_new_events(Time t) {
    size_t new_time_line_position = time_line_position;

    while(new_time_line_position < time_line.size()
          && time_line[new_time_line_position].time <= t)

    fire_events(time_line.begin() + time_line_position,
                time_line.begin() + new_time_line_position);

    time_line_position = new_time_line_position;
2012-04-04 18:33
by bames53