• Create Account

## Facilitating communication between classes C++

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Chanz  Members

Posted 23 May 2012 - 08:59 PM

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!

Edited by Chanz, 23 May 2012 - 09:01 PM.

### #2CyberRascal  Members

Posted 24 May 2012 - 03:21 AM

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 ® -> player.reload. Something like:

std::map<key_pattern, action> key_actions;

...

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

...

{
trigger()
{
}

};



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.

### #3FlyingDutchman  Members

Posted 26 May 2012 - 02:17 AM

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.

I open sourced my C++/iOS OpenGL 2D RPG engine :-)

See my blog: (Tutorials and GameDev)

http://howtomakeitin....wordpress.com/

### #4nox_pp  Members

Posted 30 May 2012 - 07:30 AM

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.

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say

### #5Dawoodoz  Members

Posted 30 May 2012 - 08:01 AM

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.

"App". is an acronym and must always be followed by a dot.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.