From e4251457fa46d22071c034e04d1f5ac53ba29593 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 24 Feb 2021 22:19:00 -0500 Subject: Added map init scripts Map scripts also now actually run in their own lua thread. --- src/script_system.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/script_system.cpp') diff --git a/src/script_system.cpp b/src/script_system.cpp index 2e02edf..d700738 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -206,15 +206,25 @@ void ScriptSystem::tick(double dt) { }); } -void ScriptSystem::runScript(std::string mapName, std::string scriptName) { +void ScriptSystem::loadMapScripts(std::string mapName) { if (!loadedScripts_.count(mapName)) { engine_.script_file("../res/scripts/" + mapName + ".lua"); loadedScripts_.insert(mapName); } +} + +bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) { + loadMapScripts(mapName); + + return !!engine_.traverse_get(mapName, scriptName); +} + +void ScriptSystem::runScript(std::string mapName, std::string scriptName) { + loadMapScripts(mapName); Script newScript; newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); - newScript.callable.reset(new sol::coroutine(engine_.traverse_get(mapName, scriptName))); + newScript.callable.reset(new sol::coroutine(newScript.runner->state().traverse_get(mapName, scriptName))); if (!*newScript.callable) { throw std::runtime_error("Error running script: " + mapName + "." + scriptName); -- cgit 1.4.1