Event handling

Started by
3 comments, last by bibalasvegas 13 years, 5 months ago
Hi, I'm trying to write an event handling system at the moment and am struggling to think of the best way to do this.

Currently I am thinking that it would be best to have an event manager with an event queue. Objects can register themselves as interested in certain types of event and events of that type will be dispatched to them when necessary by the event manager. The event queue will be sorted by time the event should be handled and then by priority if there are degeneracies.

In code I will do this by having a abstract base class "Event" from which all event types will be derived. Given that my events will be stored (not handled immediately) I will need dynamic memory allocation but am unsure of how best to achieve this - there must be a better way than having the object which sends the event to the event manager 'new' the event - ideally the event manager would allocate the memory for the event object but it will not know which of the derived event class types the event was (unless I add a long if/else check based on the event type and this doesn't seem a very easily extensible/flexible way of doing things). Perhaps there is a pattern for this kind of system?

Maybe I'm approaching the whole thing wrong and it's better to handle events immediately? Any help will be much appreciated!
Advertisement
Quote: Objects can register themselves as interested in certain types of event and events of that type will be dispatched to them when necessary by the event manager.


What you just described is the Observor design pattern. Very useful and commonly used for all kinds of things in the core engine.

Quote: ideally the event manager would allocate the memory for the event object but it will not know which of the derived event class types the event was (unless I add a long if/else check based on the event type and this doesn't seem a very easily extensible/flexible way of doing things

You can use a map to link event type id's to their respective function handler addresses. Look up the GameDev article recently posted on how to implement the system. Google this: GameDev.net -- Effective Event Handling in C++
Thanks. That example is really useful! What about the other issue though? If you are not handling events immediately then you have to store them. What is the best way to deep copy an object which is passed as a pointer to an abstract base class? I do not want to hard code each case!

I can't really think of a neat way to do this - one example I've seen just solves the problem by not having an abstract base class 'Event' at all but simply has an event class which stores the event type and several arguments which may be of variant type (or boost::any i guess?). The event handling object knows the format of the arguments so the event manager does not need to. This also seems a bit clumsy though.

Any better ways?

[Edited by - bibalasvegas on October 25, 2010 3:44:21 AM]
Quote:Original post by bibalasvegas
What is the best way to deep copy an object which is passed as a pointer to an abstract base class?

virtual BaseClass* Clone();

But why deep-copy the whole object? Why not just keep hold of the pointer?
Quote:Original post by Kylotan
virtual BaseClass* Clone();

But why deep-copy the whole object? Why not just keep hold of the pointer?


Thanks for that, it seems exactly what I was looking for.

As to why I want to deep copy...well it just doesn't seem ideal to have whatever is adding the event to the event manager 'newing' the event. If the event manager deep copies, then all the allocation and deallocation is within the same object and there will be no dangling references - seems safer. Maybe it's not necessary though and it is less efficient...

This topic is closed to new replies.

Advertisement