Game Development Reference
In-Depth Information
Always Clean Up After Yourself
The fast delegate library uses raw pointers on the inside and binds the object
pointer you give it to the function pointer. That means that if you destroy the
object without removing the delegate, you
ll get a crash when an event that
delegate is set to receive is fired. You should always remember to clean up
after yourself and remove any delegate listeners in the destructor of your
listener class.
'
While the situation is certainly not the norm, it is occasionally necessary to fire an
event and have all
listeners respond to it immediately, without using the event
queue. That
s where the VTriggerVTriggerEvent() function comes in. In all
honesty, this method breaks the paradigm of remote event handling, as you will see
done in Chapter 18,
'
s still
necessary for certain operations. A good example of a valid use is in the game startup
code when you don
Network Programming for Multiplayer Games,
but it
'
t want to wait a frame just to ensure that something started
before continuing to the next stage. Here
'
'
s the VTriggerVTriggerEvent()
method:
bool EventManager::VTriggerVTriggerEvent(const IEventDataPtr& pEvent) const
{
bool processed = false;
auto findIt = m_eventListeners.find(pEvent->VGetEventType());
if (findIt != m_eventListeners.end())
{
const EventListenerList& eventListenerList = findIt->second;
for (EventListenerList::const_iterator it = eventListenerList.begin();
it != eventListenerList.end(); ++it)
{
EventListenerDelegate listener = (*it);
listener(pEvent); // call the delegate
processed = true;
}
}
return processed;
}
This function is relatively simple. It tries to find the event listener list associated with
this event type and then, if found, iterates through all the delegates and calls each
one. It returns true if any listener handled the event.
Search WWH ::




Custom Search