Wednesday, February 9, 2011

EventHandler, you stupid beast!

Pretty much all conversation between entities of my game is done through an event system. It helps me keeping the dependencies between subsystems in control (I like spaghetti, but I can't stand having it in my code editor), also I am in good shape if someday I decide to use more than one processor core at once.
The event system as it is right now is far from being sophisticated, in fact it is only 2 simple arrays of lists of eventhandlers (one array for normal events, one for recurring ones, one list for every event type).

Here's the event handler interface:
class EventHandler
        virtual void handleEvents(int eventType, 
            const EventDataList& events) =0;
The problem with that is because most handlers handle more than one event type, I have to do tedious event type checks in almost every EventHandler instance - and I know there must be a better way!  ...TEMPLATES TO THE RESCUE!!

So guess what I did try first - yes, I thought "Let's make handleEvents a template, use the event type as template parameter and specialize that in subclasses - means one function for every event type." -- utter fail! templates can only be used on non-virtual functions.

Time for plan B...      ...tomorrow!