Jump to content
  • Advertisement
Sign in to follow this  
MilchoPenchev

Event dispatching/handling

This topic is 2496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I'm rewriting some of my old engine code to try to restructure it so its not such a mess.

Right now, my basic setup dispatches a number of events, namely, things like:


void Update(double dTime);
void Draw();
void Draw2d();
void MouseClick(sf::Mouse::Button button, sf::Mouse::State state, int x, int y);
void MouseMoved(int x, int y, int dx, int dy);
void MouseDragged(int x, int y, int dx, int dy);
void MouseWheel(int dir);
void KeysDown(sf::Event::KeyEvent key);
void KeysUp(sf::Event::KeyEvent key);
void Resize(int w, int h);
void FocusChanged (bool gained);


These functions used to be called in a trickle-down way, where my Program class, which gets the originals, would call, say, the Update function of the Game class, which would call the update for the terrain rendering class, which would call each individual object's update, and so on. You get the picture.

Then i started thinking in a different way - what if my Program class called the Update on some EventHandler class, and that EventHandler class would allow for anything to Register with it, to receive these events. So, I'd have an abstract UpdateListener, and for a class to receive the update calls, it would do something like:


class A : public UpdateListener
{
public:
A() { EventHandler::I().Register(this); }
~A() {EventHandler::I().Remove(this); }

// override the purely virtual func of UpdateListener
void Update (double dTime);
};


The thing is, i see pros and cons for both ways of handling the event dispatching, and I can't decide. A short list:

Trickle-down calls:
+ Full control over what gets called. If we want to block off a lower component from receiving events, its easy
+ No singletons (singletons are evil - well, maybe)
+ No additional classes, inheritance or code required
- No clear definitions of what the update functions are expected to look like (i.e. was that MouseClick(button, state) or was it just MouseClick(button, state, int, int) ?)
- Must manually take care to call all the necessary event functions of each child class. (see the sheer number of them in list above)

Singleton approach:
+ Clear cut definitions of what the functions need to look like
+ Each object takes care of itself for what it needs (drawing, updating, mouse, keyboard, etc)
- Singletons are evil
- Little to no control over blocking events from reaching child objects - the only thing to use would be adding a bool for event consumed, but then it depends on the registration order to the singleton

There's always the hybrid approach - for instance, I make big components like the terrain renderer register themselves, and then have them call all the necessary functions of their children, etc. This eliminates some downsides of using a singleton to which to register, and some of the trickle down method, but it still bugs me.


So, I'm curious, how have other people handled such a problem?

Share this post


Link to post
Share on other sites
Advertisement
Hi Milcho,

After reading this post, I decided to upload and describe my own C++ event handling system. I'm using templates to describe, dispatch and handle events in an intuitive manner. For example, describing an event can be done with something like this:

Event<void(std::shared_ptr<Observable>, double, double, double)> MyEvent;

, for an event that takes 4 parameters (the "sender" and three doubles). The handler looks like this:

void OnEventHandler(std::shared_ptr<Observable> sender, double x, double y, double z)
{
// Handle the event.
}


And the event is dispatched in the following intuitive way:

MyEvent(shared_from_this(), 3, 2, 1);

It's type-safe and you can dispatch any number of parameters (up to 9 actually, in the absence of variadic templates in VS2010!). Anyway the code and blog about it can be found here. I hope this is useful.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!