Reputation: 779
I'm trying to create a central event dispatching system using kind of an Observer pattern for my small RPG project, and I need advice on how best to represent event data.
I'll have an EventManager that registers listener classes for different event types defined in a static enum such as Event_Input_KeyPressed or Event_Game_ActorMoved. It will store events in a queue and dispatch them to the corresponding listeners, which will each have a method handleEvent(Event e) or something.
Each event will be an object of type Event, which holds data such as its event type.
My question is, since the nature of the event data differs considerably between event types, how should this data be represented? I'm not yet sure exactly what types of events I will want to create in the future, so I want this to be as flexible as possible. For example, an event could trigger other events if certain conditions are true (ie. when the player pressed the action key, a door opens if the player is in a certain location and has a key). Is XML or scripts a good choice? Another way I can think of is to create a custom class for every general event, like ActorEvent or MenuEvent, but that seems really inefficient and inflexible. Also, since some objects such as Character only need to know very specific events like when the "w" key is pressed, I figure they don't need to be notified when other keys like "h" are pressed. Is it viable to create event types that specific or is there a better way? ie. Event_Input_KeyPressed_W
Thanks
Upvotes: 1
Views: 602
Reputation: 1184
Upvotes: 2
Reputation: 347314
Event
base object. Firstly, it will be easier to distribute the objects - that is in your queue, you would only need to do a instanceof
to determine how to handle the object.That's MHO
Upvotes: 2