diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-10-12 08:32:21 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-10-12 08:32:21 -0400 |
commit | b709a392436d4ed17e214cd9e302ddbd23d71c21 (patch) | |
tree | 9ad8dd18d26ab6aa907b182170c08f26bea10585 /src/systems/lua.cpp | |
parent | dbf47c4e8e7731519bec212419f70e08b139be0f (diff) |
more lua scripting
Diffstat (limited to 'src/systems/lua.cpp')
-rw-r--r-- | src/systems/lua.cpp | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/src/systems/lua.cpp b/src/systems/lua.cpp index fa1e93e..e412334 100644 --- a/src/systems/lua.cpp +++ b/src/systems/lua.cpp @@ -1,25 +1,69 @@ #include <systems/lua.hpp> -void LuaScript::setGlobal(const LuaVariable& nv) +void LuaScript::setGlobal(const LuaVariable& nv) const { lua_pushnumber(state, std::get<float&>(nv)); lua_setglobal(state, std::get<std::string>(nv).c_str()); } -void LuaScript::operator()(std::vector<LuaVariable> vars) +void LuaScript::getReturns(std::vector<double>& rets) const +{ + int count = lua_gettop(state); + for (int i = 1; i <= count; i++) + rets.emplace_back(lua_tonumber(state, i)); + lua_pop(state, count); +} + +void LuaScript::operator()(const std::string& func, std::vector<LuaVariable> vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(func); + for (auto& v : vars) { + lua_getglobal(state, std::get<std::string>(v).c_str()); + std::get<float&>(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(const std::string& func, std::vector<double>& rets, + std::vector<LuaVariable> vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(func); + getReturns(rets); + for (auto& v : vars) { + lua_getglobal(state, std::get<std::string>(v).c_str()); + std::get<float&>(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(std::vector<LuaVariable> vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(); + for (auto& v : vars) { + lua_getglobal(state, std::get<std::string>(v).c_str()); + std::get<float&>(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(std::vector<double>& rets, std::vector<LuaVariable> vars) const { for (auto& v : vars) setGlobal(v); (*this)(); + getReturns(rets); for (auto& v : vars) { lua_getglobal(state, std::get<std::string>(v).c_str()); std::get<float&>(v) = lua_tonumber(state, -1); } } -void LuaScript::operator()(void) +void LuaScript::operator()(const std::string& s) const { - lua_getglobal(state, "update"); + lua_getglobal(state, s.c_str()); lua_pcall(state, 0, LUA_MULTRET, 0); } |