//--------------------------------------------------------------------------------------------------------------------- // Instantiates a C++ ScriptListener object, inserts it into the manager, and returns a handle to it. The script // should maintain the handle if it needs to remove the listener at some point. Otherwise, the listener will be // destroyed when the program exits. //--------------------------------------------------------------------------------------------------------------------- unsigned long InternalScriptExports::RegisterEventListener(EventType eventType, LuaPlus::LuaObject callbackFunction) { GCC_ASSERT(s_pScriptEventListenerMgr); if (callbackFunction.IsFunction()) { // create the C++ listener proxy and set it to listen for the event ScriptEventListener* pListener = GCC_NEW ScriptEventListener(eventType, callbackFunction); s_pScriptEventListenerMgr->AddListener(pListener); IEventManager::Get()->VAddListener(pListener->GetDelegate(), eventType); // convert the pointer to an unsigned long to use as the handle unsigned long handle = reinterpret_cast<unsigned long>(pListener); return handle; } GCC_ERROR("Attempting to register script event listener with invalid callback function"); return 0; }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool EventManager::AddScriptListener(char const * const pEventName, LuaObject callbackFunction) { //Ensure this event type exists. const EventType testEventType( pEventName ); const EventTypeSet::const_iterator typeIter = m_typeList.find( testEventType ); if ( m_typeList.end() == typeIter ) { assert( 0 && "Attempted to listen to an event type that wasn't registered!" ); return false; } const U64 eventID = testEventType.getHashValue(); //OK, valid event type. Make sure this isn't a duplicate. ScriptEventListenerMap::const_iterator mapIter = m_ScriptEventListenerMap.find( eventID ); while ( m_ScriptEventListenerMap.end() != mapIter ) { //Iterate through and ensure no duplicates. const ScriptEventListenerPtr evtListener = mapIter->second; const LuaObject & evtObj = evtListener->GetHandlerFunction(); if ( evtObj == callbackFunction ) { assert( 0 && "Attempted to listen to the same event handler twice!" ); return false; } ++mapIter; } //Now let's rez up a new script listener. ScriptEventListenerPtr listener( GCC_NEW ScriptEventListener( callbackFunction ) ); m_ScriptEventListenerMap.insert( std::make_pair( eventID, listener ) ); const bool bSuccess = VAddListener( listener, testEventType ); return bSuccess; }