static LuaPlus::LuaObject Wrap(Vec3 vector){ LuaPlus::LuaObject vecObj; vecObj.AssignNewTable(LuaManager::Get()->GetState()); vecObj.SetNumber("x", vector.x); vecObj.SetNumber("y", vector.y); vecObj.SetNumber("z", vector.z); return vecObj; }
// apply torque to an object from lua void LuaInternalScriptExports::ApplyTorque(LuaPlus::LuaObject axisLua, float force, int gameObjectId) { if (axisLua.IsTable()) { Vec3 axis(axisLua["x"].GetFloat(), axisLua["y"].GetFloat(), axisLua["z"].GetFloat()); g_pApp->m_pGame->GetGamePhysics()->ApplyTorque(axis, force, gameObjectId); return; } CB_ERROR("Invalid object passed to ApplyForce(). Type = " + std::string(axisLua.TypeName())); }
void LuaScriptExports::registerScripts( void ) { LuaPlus::LuaObject globals = LuaStateManager::instance()->getGlobalVars(); // Init InternalLuaScriptExports::init(); // Resource loading globals.RegisterDirect("loadAndExecuteScriptResource",InternalLuaScriptExports::loadAndExecutreScriptResource); // Process manager globals.RegisterDirect("attachProcess",&InternalLuaScriptExports::attachScriptProcess); // Event system globals.RegisterDirect("registerEventListener", &InternalLuaScriptExports::registerEventListener); globals.RegisterDirect("removeEventListener", &InternalLuaScriptExports::removeEventListener); globals.RegisterDirect("queueEvent", &InternalLuaScriptExports::queueEvent); globals.RegisterDirect("triggerEvent", &InternalLuaScriptExports::triggerEvent); // Actors globals.RegisterDirect("createActor", &InternalLuaScriptExports::createActor); // Lua log globals.RegisterDirect("log",&InternalLuaScriptExports::lualog); }
static LuaPlus::LuaObject Wrap(Quat quat){ LuaPlus::LuaObject quatObj; quatObj.AssignNewTable(LuaManager::Get()->GetState()); quatObj.SetNumber("w", quat.w); quatObj.SetNumber("x", quat.x); quatObj.SetNumber("y", quat.y); quatObj.SetNumber("z", quat.z); return quatObj; }
// write to the log from lua script void LuaInternalScriptExports::LuaLog(LuaPlus::LuaObject text) { if (text.IsConvertibleToString()) { CB_LOG("Lua", text.ToString()); } else { CB_LOG("Lua", "<" + std::string(text.TypeName()) + ">"); } }
void ant::InternalLuaScriptExports::lualog( LuaPlus::LuaObject text ) { if (text.IsConvertibleToString()) { GCC_LOG("Lua",text.ToString()); } else { GCC_LOG("Lua","<" + std::string(text.TypeName()) + ">"); } }
LuaPlus::LuaObject BaseScriptComponent::GetLookAt(void) const { LuaPlus::LuaObject ret; shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name)); if (pTransformComponent) LuaStateManager::Get()->ConvertVec3ToTable(pTransformComponent->GetLookAt(), ret); else ret.AssignNil(LuaStateManager::Get()->GetLuaState()); return ret; }
void InternalScriptExports::AttachScriptProcess(LuaPlus::LuaObject scriptProcess) { LuaPlus::LuaObject temp = scriptProcess.Lookup("__object"); if (!temp.IsNil()) { shared_ptr<Process> pProcess(static_cast<Process*>(temp.GetLightUserData())); g_pApp->m_pGame->AttachProcess(pProcess); } else { GCC_ERROR("Couldn't find __object in script process"); } }
// get the y rotation in radians from a lua vec3 float LuaInternalScriptExports::GetYRotationFromVector(LuaPlus::LuaObject vec3) { // make sure vec3 is a table if (vec3.IsTable()) { Vec3 lookAt(vec3["x"].GetFloat(), vec3["y"].GetFloat(), vec3["z"].GetFloat()); // use the GetYRotationFromVector from math.h return ::GetYRotationFromVector(lookAt); } CB_ERROR("Invalid object passed to GetYRotationFromVector(); type = " + std::string(vec3.TypeName())); return 0; }
void ant::InternalLuaScriptExports::attachScriptProcess( LuaPlus::LuaObject scriptProcess ) { LuaPlus::LuaObject temp = scriptProcess.Lookup("__object"); if (!temp.IsNil()) { IProcessStrongPtr pProcess(static_cast<IProcess*>(temp.GetLightUserData())); ProcessManagerSingleton::instance()->getProcessManager()->attachProcess(pProcess); } else { GCC_ERROR("Could not find __object in script proces"); } }
LuaPlus::LuaObject LuaScriptComponent::GetPos() { // return the objects position to lua LuaPlus::LuaObject ret; shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name)); if (pTransformComponent) LuaStateManager::Get()->ConvertVec3ToTable(pTransformComponent->GetPosition(), ret); else ret.AssignNil(LuaStateManager::Get()->GetLuaState()); return ret; }
//--------------------------------------------------------------------------------------------------------------------- // This function is called to register an event type with the script to link them. The simplest way to do this is // with the REGISTER_SCRIPT_EVENT() macro, which calls this function. //--------------------------------------------------------------------------------------------------------------------- void ScriptEvent::RegisterEventTypeWithScript(const char* key, EventType type) { // get or create the EventType table LuaPlus::LuaObject eventTypeTable = LuaStateManager::Get()->GetGlobalVars().GetByName("EventType"); if (eventTypeTable.IsNil()) eventTypeTable = LuaStateManager::Get()->GetGlobalVars().CreateTable("EventType"); // error checking GCC_ASSERT(eventTypeTable.IsTable()); GCC_ASSERT(eventTypeTable[key].IsNil()); // add the entry eventTypeTable.SetNumber(key, (double)type); }
// create a lua vec3 table from an angle in radians LuaPlus::LuaObject LuaInternalScriptExports::GetVectorFromRotation(float angleRadians) { // use the GetVectorFromYRotation from math.h Vec3 result = ::GetVectorFromYRotation(angleRadians); // create a lua vec3 table from the result LuaPlus::LuaObject luaResult; luaResult.AssignNewTable(LuaStateManager::Get()->GetLuaState()); luaResult.SetNumber("x", result.x); luaResult.SetNumber("y", result.y); luaResult.SetNumber("z", result.z); return luaResult; }
void BaseScriptComponent::CreateScriptObject(void) { LuaStateManager* pStateMgr = LuaStateManager::Get(); AC_ASSERT(pStateMgr); AC_ASSERT(!m_scriptObject.IsNil()); LuaPlus::LuaObject metaTableObj = pStateMgr->GetGlobalVars().Lookup(METATABLE_NAME); AC_ASSERT(!metaTableObj.IsNil()); LuaPlus::LuaObject boxedPtr = pStateMgr->GetLuaState()->BoxPointer(this); boxedPtr.SetMetaTable(metaTableObj); m_scriptObject.SetLightUserData("__object", this); m_scriptObject.SetMetaTable(metaTableObj); }
void LuaScriptEventManager::RegisterEventTransformer(LuaScriptEventTransformation transformer, EventType type, LPCSTR name) { LuaScript* luas = (LuaScript*)chimera::g_pApp->GetScript(); LuaPlus::LuaObject table = luas->GetState()->GetGlobals().GetByName("EventType"); if(table.IsNil()) { table = luas->GetState()->GetGlobals().CreateTable("EventType"); } table.SetNumber(name, type); m_transformMap[type] = transformer; }
void LuaScriptComponent::CreateScriptObject() { LuaStateManager* pStateManager = LuaStateManager::Get(); CB_ASSERT(pStateManager); CB_ASSERT(!m_ScriptObject.IsNil()); LuaPlus::LuaObject metaTableObj = pStateManager->GetGlobalVars().Lookup(LUA_METATABLE_NAME); CB_ASSERT(!metaTableObj.IsNil()); // bind the __object field in lua to this object LuaPlus::LuaObject boxedPtr = pStateManager->GetLuaState()->BoxPointer(this); boxedPtr.SetMetaTable(metaTableObj); m_ScriptObject.SetLightUserData("__object", this); m_ScriptObject.SetMetaTable(metaTableObj); }
void LuaPlusGCFunction(void* s) { lua_State* L = (lua_State*)s; LuaPlus::LuaState* state = lua_State_To_LuaState(L); if (!state) return; global_State* g = G(L); LuaPlus::LuaObject* curObj = (LuaPlus::LuaObject*)G(L)->gchead_next; while (curObj != (LuaPlus::LuaObject*)&G(L)->gctail_next) { markvalue(g, curObj->GetTObject()); curObj = *(LuaPlus::LuaObject**)curObj; } }
LuaPlus::LuaObject BaseScriptComponent::GetActorId(void) { //GCC_LOG("ObjectSystem", "BaseScriptComponent::GetEntityId() return 0x" + ToStr(m_pOwner->GetId(), 16) + " on C++ side"); // [rez] Wtf?? Returning an unsigned int here will produce really odd results in Release mode (debug is // fine). The number will sometimes get truncated, so 0xb1437643 might become 0xb1437600. Even though the // C++ side is getting the right number, it gets mangled on the way out to Lua. The somewhat hacky fix here // is to return a LuaPlus::LuaObject, which seems to work just fine. I'm not sure if this will cause any // efficiency issues, but I'm guessing the LuaPlus internals do something like this anyway to marshal the // data back to Lua land, so it's probably okay. LuaPlus::LuaObject ret; ret.AssignInteger(LuaStateManager::Get()->GetLuaState(), m_pOwner->GetId()); return ret; //return m_pOwner->GetId(); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// LuaStateManager::LuaStateManager(void) : m_GlobalState(true) { //Create our metatable... m_MetaTable = m_GlobalState->GetGlobals().CreateTable("LuaStateManagerMetaTable"); m_MetaTable.SetObject("__index", m_MetaTable); // Here we register two functions to make them accessible to script. m_MetaTable.RegisterObjectDirect("DoFile", (LuaStateManager *)0, &LuaStateManager::DoFile); m_MetaTable.RegisterObjectDirect("DoResourceFile", (LuaStateManager *)0, &LuaStateManager::DoResourceFile); m_MetaTable.RegisterObjectDirect("PrintDebugMessage", (LuaStateManager *)0, &LuaStateManager::PrintDebugMessage); LuaPlus::LuaObject luaStateManObj = m_GlobalState->BoxPointer(this); luaStateManObj.SetMetaTable(m_MetaTable); // And here we expose the metatable as a named entity. m_GlobalState->GetGlobals().SetObject("LuaStateManager", luaStateManObj); }
void BaseScriptComponent::RegisterScriptFunctions(void) { // create the metatable LuaPlus::LuaObject metaTableObj = LuaStateManager::Get()->GetGlobalVars().CreateTable(METATABLE_NAME); metaTableObj.SetObject("__index", metaTableObj); // transform component functions metaTableObj.RegisterObjectDirect("GetActorId", (BaseScriptComponent*)0, &BaseScriptComponent::GetActorId); metaTableObj.RegisterObjectDirect("GetPos", (BaseScriptComponent*)0, &BaseScriptComponent::GetPos); metaTableObj.RegisterObjectDirect("SetPos", (BaseScriptComponent*)0, &BaseScriptComponent::SetPos); metaTableObj.RegisterObjectDirect("GetLookAt", (BaseScriptComponent*)0, &BaseScriptComponent::GetLookAt); metaTableObj.RegisterObjectDirect("GetYOrientationRadians", (BaseScriptComponent*)0, &BaseScriptComponent::GetYOrientationRadians); metaTableObj.RegisterObjectDirect("RotateY", (BaseScriptComponent*)0, &BaseScriptComponent::RotateY); metaTableObj.RegisterObjectDirect("Stop", (BaseScriptComponent*)0, &BaseScriptComponent::Stop); metaTableObj.RegisterObjectDirect("SetPosition", (BaseScriptComponent*)0, &BaseScriptComponent::SetPosition); }
bool LoadScene(const std::string &sceneName) { std::string fullPath; fullPath = "Scenes/"; fullPath += sceneName; fullPath += ".lua"; LuaPlus::LuaStateAuto state; state = LuaPlus::LuaState::Create(); int retVal = state->DoFile(fullPath.c_str()); _ASSERT(retVal==0); LuaPlus::LuaObject meshList = state->GetGlobals()["MeshList"]; _ASSERT(meshList.IsTable()); int numMeshes = meshList.GetCount(); for(int meshIndex=1;meshIndex <= meshList.GetCount(); meshIndex++) { LuaPlus::LuaObject meshObj = meshList[meshIndex]; LuaPlus::LuaObject nameObj = meshObj["Name"]; _ASSERT(nameObj.IsString()); std::string meshName = nameObj.GetString(); MeshManager::GetInstance().Load(meshName,meshObj); Instance *inst = InstanceManager::GetInstance().CreateInstance(meshName); inst->SetMeshName(meshName); } return true; }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// void LuaStateManager::PrintDebugMessage(LuaPlus::LuaObject debugObject) { LuaPlus::LuaObject stringObj; const char * pFinalStr = debugObject.ToString(); // Generate an event. const EvtData_Debug_String debugEvent((NULL == pFinalStr) ? "INVALID!" : pFinalStr, EvtData_Debug_String::kDST_ScriptMsg); safeTriggerEvent(debugEvent); }
void TransformationComponent::RegisterScriptFunction() { LuaPlus::LuaObject metaTable = LuaManager::GetSingleton().GetGlobalVars().CreateTable("TransformationComponentMetatable"); metaTable.SetObject("__index", metaTable); //TODO -> Studente MGD //Esempio: metaTable.RegisterObjectDirect("NomeFunzioneInLua", (NomeClass*)0, Puntatore a funzione); // Esporre SetPosition // Esporre GetPosition // Esporre Translate // Esporre LookAt // Esporre Rotate // Esporre GetRotateX // Esporre GetRotateY // Esporre GetRotateZ }
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 LuaScriptComponent::RegisterScriptFunctions() { // create the meta table LuaPlus::LuaObject metaTableObj = LuaStateManager::Get()->GetGlobalVars().CreateTable(LUA_METATABLE_NAME); metaTableObj.SetObject("__index", metaTableObj); // bind functions to lua object metaTableObj.RegisterObjectDirect("GetObjectId", (LuaScriptComponent*)0, &LuaScriptComponent::GetObjectId); metaTableObj.RegisterObjectDirect("GetPos", (LuaScriptComponent*)0, &LuaScriptComponent::GetPos); metaTableObj.RegisterObjectDirect("SetPos", (LuaScriptComponent*)0, &LuaScriptComponent::SetPos); metaTableObj.RegisterObjectDirect("GetLookAt", (LuaScriptComponent*)0, &LuaScriptComponent::GetLookAt); metaTableObj.RegisterObjectDirect("GetYOrientationRadians", (LuaScriptComponent*)0, &LuaScriptComponent::GetYOrientationRadians); metaTableObj.RegisterObjectDirect("RotateY", (LuaScriptComponent*)0, &LuaScriptComponent::RotateY); metaTableObj.RegisterObjectDirect("Stop", (LuaScriptComponent*)0, &LuaScriptComponent::Stop); metaTableObj.RegisterObjectDirect("SetPosition", (LuaScriptComponent*)0, &LuaScriptComponent::SetPosition); }
void AEngineFramework::Desc::deserialize(lua::State* luaState, const LuaPlus::LuaObject& src){ // Logging LuaObject loggingTable = src.Get("logging"); if(loggingTable.IsTable()){ luaConv(loggingTable.Get("filePath"), logging.filePath); luaConv(loggingTable.Get("enabled"), logging.enabled); } // Layout LuaObject windowTable = src.Get("window"); window.deserialize(luaState, windowTable); // File system LuaObject fileSysTable = src.Get("file_system"); fileSystem.deserialize(luaState, fileSysTable); // Misc luaConv(src.Get("home_dir"), homeDir); luaConv(src.Get("main_loop_step"), mainLoopStep); }
float InternalScriptExports::GetYRotationFromVector(LuaPlus::LuaObject vec3) { if (vec3.IsTable()) { Vec3 lookAt(vec3["x"].GetFloat(), vec3["y"].GetFloat(), vec3["z"].GetFloat()); return ::GetYRotationFromVector(lookAt); } GCC_ERROR("Invalid object passed to GetYRotationFromVector(); type = " + std::string(vec3.TypeName())); return 0; }
bool EvtData_EndAccelerating::buildEventFromScript(void) { if (m_eventData.IsTable()) { // ID LuaPlus::LuaObject temp = m_eventData.GetByName("id"); if (temp.IsInteger()) { m_id = temp.GetInteger(); } else { GCC_ERROR("Invalid id sent to EvtData_EndAccelerating; type = " + std::string(temp.TypeName())); return false; } return true; } return false; }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool SetVector3FromLua(const LuaPlus::LuaObject &dirData, Vector3 &direction) { if(!dirData.IsTable()) { return (false); } LuaPlus::LuaObject xData = dirData["x"]; LuaPlus::LuaObject yData = dirData["y"]; LuaPlus::LuaObject zData = dirData["z"]; if(!xData.IsNumber() || !yData.IsNumber() || !zData.IsNumber()) { return (false); } F32 x = static_cast<F32>(xData.GetNumber()); F32 y = static_cast<F32>(yData.GetNumber()); F32 z = static_cast<F32>(zData.GetNumber()); direction.Set(x, y, z); return (true); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool SetPoint3FromLua(const LuaPlus::LuaObject &posData, Point3 &position) { if(!posData.IsTable()) { return (false); } LuaPlus::LuaObject xData = posData["x"]; LuaPlus::LuaObject yData = posData["y"]; LuaPlus::LuaObject zData = posData["z"]; if(!xData.IsNumber() || !yData.IsNumber() || !zData.IsNumber()) { return (false); } F32 x = static_cast<F32>(xData.GetNumber()); F32 y = static_cast<F32>(yData.GetNumber()); F32 z = static_cast<F32>(zData.GetNumber()); position.Set(x, y, z); return (true); }