void ScriptEventListener::ScriptEventDelegate(IEventDataPtr pEvent) { GCC_ASSERT(m_scriptCallbackFunction.IsFunction()); // this should never happen since it's validated before even creating this object // call the Lua function shared_ptr<ScriptEvent> pScriptEvent = static_pointer_cast<ScriptEvent>(pEvent); LuaPlus::LuaFunction<void> Callback = m_scriptCallbackFunction; Callback(pScriptEvent->GetEventData()); }
void ScriptOnUpdate(ActorId id, const util::Vec3& position, int levelSize) { LuaPlus::LuaObject o = chimera::g_pApp->GetScript()->GetState()->GetGlobal("OnUpdate"); if(!o.IsNil() && o.IsFunction()) { LuaPlus::LuaFunction<void> function = chimera::g_pApp->GetScript()->GetState()->GetGlobal("OnUpdate"); function(chimera::script::GetIntegerObject(id), chimera::script::GetVec3Object(position), chimera::script::GetIntegerObject(levelSize)); } }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// void LuaStateManager::IdentifyLuaObjectType(LuaPlus::LuaObject &objToTest) { assert(!objToTest.IsNil() && "Nil!"); assert(!objToTest.IsBoolean() && "Boolean!"); assert(!objToTest.IsCFunction() && "C-Function!"); assert(!objToTest.IsFunction() && "Function!"); assert(!objToTest.IsInteger() && "Integer!"); assert(!objToTest.IsLightUserData() && "Light User Data!"); assert(!objToTest.IsNone() && "None!"); assert(!objToTest.IsNumber() && "Number!"); assert(!objToTest.IsString() && "String!"); assert(!objToTest.IsTable() && "Table!"); assert(!objToTest.IsUserData() && "User Data!"); assert(!objToTest.IsWString() && "Wide String!"); assert(0 && "UNKNOWN!"); }
ant::Ulong ant::InternalLuaScriptExports::registerEventListener( EventType eventType, LuaPlus::LuaObject callbackFunction ) { GCC_ASSERT(s_ScriptEventListenerMgrInstance); if (callbackFunction.IsFunction()) { // Create the C++ listener proxy and set it to listen for the event ScriptEventListener* pListener = GCC_NEW ScriptEventListener(eventType, callbackFunction); s_ScriptEventListenerMgrInstance->addListener(pListener); IEventManager::instance()->addListener(pListener->getDelegate(), eventType); // Convert the pointer to an unsigned log to use as a handle ant::Ulong handle = reinterpret_cast<ant::Ulong>(pListener); return handle; } GCC_ERROR("Attempting to register script event listener with invalid callback function"); return 0; }
//--------------------------------------------------------------------------------------------------------------------- // 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; }
// add a lua listener callback for an event type. this will still be inserted into the C++ event manager // which will fire events that are forward to the lua callback unsigned long LuaInternalScriptExports::RegisterEventListener(EventType eventType, LuaPlus::LuaObject callbackFunction) { CB_ASSERT(s_pScriptEventListenerMgr); // make sure the lua object is a function if (callbackFunction.IsFunction()) { // create C++ proxy listener to listen for the event. this will forward the events to the lua callback LuaScriptEventListener* pListener = CB_NEW LuaScriptEventListener(eventType, callbackFunction); // add the listener to the lua listener manager s_pScriptEventListenerMgr->AddListener(pListener); // add its delegate listener function to the C++ event manager. this is where the events actually fire from IEventManager::Get()->AddListener(pListener->GetDelegate(), eventType); // conver the pointer to an unsigned long and use that as a handle unsigned long handle = reinterpret_cast<unsigned long>(pListener); return handle; } CB_ERROR("Invalid callback. Make sure the lua listener is a function"); return 0; }
void LuaManager::PrintTable(LuaPlus::LuaObject table, bool recursive) { if(table.IsNil()) { if(!recursive) GLIB_LOG("LuaManager", "null table"); return; } if(!recursive) GLIB_LOG("LuaManager", "PrintTable()"); cout << (!recursive ? "--\n" : "{ "); bool noMembers = true; for(LuaPlus::LuaTableIterator iter(table); iter; iter.Next()) { noMembers = false; LuaPlus::LuaObject key = iter.GetKey(); LuaPlus::LuaObject value = iter.GetValue(); cout << key.ToString() << ": "; if(value.IsFunction()) cout << "function" << "\n"; else if(value.IsTable()) PrintTable(value, true); else if(value.IsLightUserData()) cout << "light user data" << "\n"; else cout << value.ToString() << ", "; } cout << (!recursive ? "\n--" : "}"); cout << endl; }