Hey,
have you thought about using something like a MessageQueue ? This is an awesome concept as it allows you to communicate between classes without them knowing about each other and it works very nicely in multithreaded environment if you get your locks right.
I use it mostly like this:
class Message {
// i dont do anything :-)
};
class MessageQueue {
public:
MessageQueue(std::function<void(const Message &msg)> cb) : callback_(cb) {}
void PostMessage(Message *msg) { queue_.push_back (shared_ptr(msg)) };
void Update() { for (Message msg : queue_) { callback_(*msg) } queue_.clear()};
private:
std::vector<shared_ptr<Message>> queue_;
std::function<void (const Message& msg)> callback_;
};
Thats a bit how the interface looks like.
Now , every runloop call you call Update. and the queue will evaluate messages in the vector.
A custom message you can make if you derive from the Message class. For example like this:
class LayBombMessage : public Message {
public:
LayBombMessage(Player* p) : p_(p) {}
inline const Player& Player() { return *p_ } const;
private:
Player *p_;
};
Now when a player hits space you do this
MainQueue.PostMessage(new LayBombMessage(playerWhoDidIt));
and than in the update callback_ you can for example do s.th. like this.
void EntityManager::HandleMessages(const Message& msg) {
LayBombMessage *lbm = dynamic_cast<LayBombMessage>(msg);
if (lbm) {
LayBomb(lbm->Player());
}
}
Hope that helped a bit :-)
The cool thing is, that you can post Messages from every thread you want and it will evaluate them in the thread you call MyQueue.Update(). I use that a lot in Server environments. The example above needs only one additional mutex_ and two Lock/Unlock calls to work in a multithreading environment.
Edited by FlyingDutchman, 26 May 2012 - 02:23 AM.