• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DanielWilkins

Facilitating communication between classes C++

4 posts in this topic

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:[list]
[*]--EntityManager
[*]-Player
[*]-Bombs
[/list]
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
0

Share this post


Link to post
Share on other sites
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:

[CODE]
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();
}

};

[/CODE]

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. :)
1

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0