void SceneLuaScript::Update() { float dt = TheTimer::Instance()->GetDt(); // Call lua update() function Lua::LuaFuncName funcName = "update"; // Pass in Matrix and dt VariableVec args; args.push_back(ToVariable(m_local)); args.push_back(dt); Variable retvals; // Expect 16 floats back if (!m_lua.Call(funcName, args, &retvals, 16)) { std::cout << "Failed to call function '" << funcName << "'\n"; Assert(0); return; } // Retval is new matrix Assert(retvals.IsVectorType()); Assert(retvals.GetVector().size() == 16); m_local = ToMatrix(retvals); std::cout << "New matrix: " << m_local << "\n"; }
// Push args onto Lua stack. // Used by Call() and Return() below // Returns no of args pushed. // Called recursively to flatten vectors of variables static int PushLuaArgs(lua_State* L, const Variable& v) { int numPushed = 0; if (v.IsIntType()) { int intArg = v.GetInt(); lua_pushnumber(L, intArg); numPushed = 1; } else if (v.IsFloatType()) { float fArg = v.GetFloat(); lua_pushnumber(L, fArg); numPushed = 1; } else if (v.IsStringType()) { lua_pushstring(L, v.GetString().c_str()); numPushed = 1; } else if (v.IsVectorType()) { VariableVec vec = v.GetVector(); int numArgs = vec.size(); for (int i = 0; i < numArgs; i++) { const Variable& vv = vec[i]; numPushed += PushLuaArgs(L, vv); } } else { // TODO std::cout << "LUA: c++: Type not handled yet! Get on it dude!\n"; Assert(0); } return numPushed; }