Game Development Reference
In-Depth Information
First, the code grabs (or creates) the event listener list. It walks through the list to see
if the listener has already been registered and kicks out a warning if it has been. Reg-
istering the same delegate for the same event more than once is definitely an error,
since processing the event would end up calling the delegate function multiple times.
If the delegate has never been registered for this event, it
'
s added to the list.
Here
'
s how you remove a listener:
bool EventManager::VRemoveListener(const EventListenerDelegate& eventDelegate,
const EventType& type)
{
bool success = false;
auto findIt = m_eventListeners.find(type);
if (findIt != m_eventListeners.end())
{
EventListenerList& listeners = findIt->second;
for (auto it = listeners.begin(); it != listeners.end(); ++it)
{
if (eventDelegate == (*it))
{
listeners.erase(it);
success = true;
// We don
t need to continue because it should be impossible for
// the same delegate function to be registered for the same event
// more than once.
break;
'
}
}
}
return success;
}
This function gets the event listener list for the event type that was passed in. If the
list is valid, it walks through it, attempting to find the delegate. The FastDelegate
classes all implement an overloaded == operator, so this works really well. If the del-
egate is found, it
'
s removed from the list, success is set to true , and the loop is
broken. That
'
s all there is to it.
Search WWH ::




Custom Search