added startScript()

This commit is contained in:
CPunch 2023-12-30 01:53:07 -06:00
parent c5de584762
commit d32dd4c319
2 changed files with 16 additions and 2 deletions

View File

@ -10,10 +10,21 @@ void LuaManager::init() {
lua_State *L = luaL_newstate(); lua_State *L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
if (luaL_dostring(L, "print(\"Hello from Lua!\")")) { static LuaThread *startScript(const char *script) {
lua_State *L = lua_newthread(globalState);
LuaThread *T = new LuaThread(L, luaL_ref(L, LUA_REGISTRYINDEX));
if (luaL_dostring(L, script)) {
std::cout << "Lua error: " << lua_tostring(L, -1) << std::endl; std::cout << "Lua error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1); lua_pop(L, 1);
} }
globalState = L; return T;
}
void LuaManager::init() {
globalState = luaL_newstate();
luaL_openlibs(globalState);
delete startScript("print(\"Hello from Lua!\")");
} }

View File

@ -11,4 +11,7 @@ struct LuaThread {
int ref; int ref;
LuaThread(lua_State *L, int ref) : L(L), ref(ref) {} LuaThread(lua_State *L, int ref) : L(L), ref(ref) {}
~LuaThread() {
luaL_unref(L, LUA_REGISTRYINDEX, ref);
}
}; };