mercredi 6 janvier 2016

Create queue of functions to be called later by another object

I'm working on a game loop using a set of "manager" objects, each with their own update() function that is called every iteration of the game loop. For example:

while(true)
{
    EventManager::update();
    ObjectManager::update();
    SoundManager::update();
    VideoManager::update();
}

Where each manager deals with its own delegated tasks. For example, EventManager might handle any key presses, while SoundManager would play deal with sounds and music.

Suppose I were to want a sound to play whenever the player hits the Space bar. I would like to have the key press handled by the Event manager object and the sound playing to be handled by the sound manager.

I could simply call some method playSound() in SoundManager so that it plays immediately upon hitting the space bar, but I would prefer to enqueue it instead, so that the sound is played during SoundManager::update() is called.

What I'm thinking of doing right now is giving each manager a vector of strings that would be treated as commands, so that if I want to enqueue the method

playSound("Foo.wav", false, 3)

I could simply add the string

"playSound-Foo.wav-false-3" 

to the Sound Manager's update queue, where the request could be parsed during the Sound Manager's update method. The problem I see with this approach is that it doesn't seem very easily expandable, and it seems like a bit of a waste of CPU time to have to go through parsing potentially hundreds or thousands of commands per frame. Is there an easier or more efficient approach to this?

Aucun commentaire:

Enregistrer un commentaire