Re: Modeling events that occur in a game world




"Aaron J. M." wrote:

O.K. However, if I'm going to have to do casting and have a bunch of
switch statements anyway, what's wrong with:

if e instanceof AttackEventArgs
AttackEventArgs attackEvent = (AttackEventArgs)e
else if ...

I'm only asking because using instanceof seems a bit easier. Would
the ID number be better for performance though?

When the event queue dequeues an event and needs to notify the objects
that have registered for the event, it will need a key of some type to
retreive the list of registered objects. An integer would be ideal. So
when the event queue dequeues an event, it uses the event's ID to
retreive a list of objects interested in that event:

GameEventArgs e = queue.Dequeue();
List list = listeners.GetValue(e.ID);

foreach(Listener l in list)
{
l.ProcessEvent(e);
}

When an object registers to be notified when a specific type of event
occurs, it is placed in the event queue's dictionary of listeners using
the event's ID as the key. Something like this:

public void Register(int eventID, GameListener listener)
{
List list = listeners.GetValue(eventID);

list.Add(listener);
}

However, whether you use the ID number or instanceof in the switch
statements in the listener's ProcessEvent method is up to you. I don't
know if one is more efficient than the other.



.