Not sure if this is the right place to ask about this, but it seemed the best option.
I'm trying to write my own event manager for a game i'm working on, but it's acting very strange. It works great when I've got just two events being listened to, but it freaks out when I add a third.
The Manager:
Display All
The Listeners:
(Logic)
(HumanView)
The two 'this' pointers are to the logic class, which is derived from IListener. The 'view' pointer is the HumanView, which is also derived from IListener.
When the code is run like that, it sends the detach event to the HumanView, not the Logic. When I comment the DetachSceneEvent line it works fine. When I comment the QuitEvent line, it sends the ButtonDownEvent to the Logic instead.
I have no clue what is going on. If anyone has any tips they would be greatly appreciated.
[Edit]
So if I add a bunch of random characters to the base strings of the Quit and DetachScene events, it works. I don't know why. I've checked to make sure that the register/check registered routines work and they do, so I don't know what's happening.
I'm trying to write my own event manager for a game i'm working on, but it's acting very strange. It works great when I've got just two events being listened to, but it freaks out when I add a third.
The Manager:
Source Code
- void EventManager::RegisterEventType(HashedString str)
- {
- for(unsigned int a = 0; a < RegisteredEvents.size(); a++)
- {
- if(str == RegisteredEvents[a])
- {
- printf("Event Type %s Already Registered!\n", str.getStr());
- return;
- }
- }
- RegisteredEvents.push_back(str);
- }
- bool EventManager::CheckEventRegistered(HashedString str)
- {
- for(unsigned int a = 0; a < RegisteredEvents.size(); a++)
- {
- if(str == RegisteredEvents[a])
- return true;
- }
- printf("Warning: Event %s Not Registered!\n", str.getStr());
- return false;
- }
- void EventManager::Tick()
- {
- while ( Queue.size() > 0 )
- {
- // Get an event from the queue
- IEvent* nEvent = Queue.back();
- Queue.pop_back();
- // Get the list of listeners attached to that event
- EventListenerMap::Iterator i = ListenerMap.find(nEvent->getType());
- // If there aren't any listeners for this event, start over
- if(i.atEnd())
- continue;
- ListenerList list = (*i).getValue();
- // Iterate through the listeners and hand them the event.
- for(ListenerList::Iterator it = list.begin(); it!=list.end(); it++)
- {
- if((*it)->HandleEvent( nEvent ))
- break;
- }
- delete nEvent;
- }
- }
- void EventManager::QueueEvent(IEvent* evnt)
- {
- // Add an event to the queue
- curQueue->push_front(evnt);
- }
- void EventManager::AddListener(HashedString evnt, IListener *listener)
- {
- if(!CheckEventRegistered(evnt))
- return;
- // Find the event type
- EventListenerMap::Iterator it = ListenerMap.find(evnt);
- // If we don't have a map entry for this event type already, create a new one.
- if(it.atEnd())
- {
- ListenerList list;
- list.push_back(listener);
- ListenerMap.insert(evnt, list);
- return;
- }
- // Attach a listener to an event
- (*it).getValue().push_back(listener);
- }
The Listeners:
(Logic)
(HumanView)
The two 'this' pointers are to the logic class, which is derived from IListener. The 'view' pointer is the HumanView, which is also derived from IListener.
When the code is run like that, it sends the detach event to the HumanView, not the Logic. When I comment the DetachSceneEvent line it works fine. When I comment the QuitEvent line, it sends the ButtonDownEvent to the Logic instead.
I have no clue what is going on. If anyone has any tips they would be greatly appreciated.
[Edit]
So if I add a bunch of random characters to the base strings of the Quit and DetachScene events, it works. I don't know why. I've checked to make sure that the register/check registered routines work and they do, so I don't know what's happening.
The post was edited 5 times, last by Nethara ().