Skip to main content
GameDev.net gamedev.net
🔒 Locked

Facilitating communication between classes C++

Started by DanielWilkins May 24, 2012 at 2:59 AM 3 replies 5.7k views
Original Post
DanielWilkins
DanielWilkins
Hi. I am writing a Bomberman clone and would like some advice on the way most 2d engines communication between classes (still preserving object oriented class design). For instance, I wrote an entity manager which contains all of the main classes for the game. It's basically an encapsulation of the game to take it out of the main.cpp file. It handles updating all of the classes (players, bombs, items).
Now I have gotten to an interesting point. I had the handleInput() function in my main.cpp file but have since moved it into my class manager. I guess my question is if there is an agreed upon way to have two classes communicate with eachother. For example, if I were to handle input in the player class for each player individually and the player hits space bar, what would be the best way to alert the bomb class to create a new bomb at the location provided by the player. Would some sort of event queue be the way to go. Here is a simple visualization of the classes:

  • --EntityManager
  • -Player
  • -Bombs

    I want to keep the EntityManager as abstract as possible for reuse in other games so while updatePlayers() and updateBombs() (functions that invoke the update routine by each class) is fine by me, I want to shy away from things like addBomb() and more really specific stuff which should be in each class.
    Does anyone have any advice on this matter? Or maybe a good design document to look at?
    Thanks a bunch!
CyberRascal
CyberRascal
Hi, I've got a few comments, take it for what it's worth (possibly nothing ;) )

It sounds like EntityManager is somewhat of a god object, that is, it seems like it does a lot of things? I'll let you use your own judgement here, but it's a good approach to separate concerns as much as possible.

'the handleInput function'? What does that function actually do? A clean design would *not* put input stuff in Player, unless Player is a badly named input-handler only.

I'd look into using something like a map or something that maps (keys + optionally modifiers) -> actions, for example (R) -> player.reload. Something like:


std::map<key_pattern, action> key_actions;

key_actions.add('R', reload_action(player));

...

if(key_actions.contains(key))
key_actions[key].trigger();

...

class reload_action
{
reload_action(player &player);
trigger()
{
player.attempt_reload();
}

};



Syntax is not 100% valid, kinda pseudo-c++.

Also, UpdateBombs isn't very generic either through, is it? You could simply have a interface updateable with a method update, or similar.
MarkusPfundstein
MarkusPfundstein
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 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> queue_;
std::function 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(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.
nox_pp
nox_pp
If you won't be utilizing any of the additional features granted by a message queue (scheduling,easier multithreading,) then CyberRascal's solution is the way to go. Personally, I wouldn't bother using functors though. I would just bind to functions with boost::bind & boost::function. FWIW, I generally fail to see the value of message passing in the presence of straight function calls, especially on the small scale.
Dawoodoz
Dawoodoz
I use static allocations of classes that recieve calls in the main class that items can point back to. This is like multiple inheritance but without exposing local variables. In most cases, I try to separate methods from data to make the calls go in the same direction and improve reusability.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.