summary refs log tree commit diff stats
path: root/src/script_system.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-24 22:19:00 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-24 22:19:00 -0500
commite4251457fa46d22071c034e04d1f5ac53ba29593 (patch)
tree8b61201a9a2d109041a298fa8ae43ae2f03cda3f /src/script_system.cpp
parent4effe126d5b04d7e8572f8d785735a66150aa4ee (diff)
downloadtanetane-e4251457fa46d22071c034e04d1f5ac53ba29593.tar.gz
tanetane-e4251457fa46d22071c034e04d1f5ac53ba29593.tar.bz2
tanetane-e4251457fa46d22071c034e04d1f5ac53ba29593.zip
Added map init scripts
Map scripts also now actually run in their own lua thread.
Diffstat (limited to 'src/script_system.cpp')
-rw-r--r--src/script_system.cpp14
1 files changed, 12 insertions, 2 deletions
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) {
206 }); 206 });
207} 207}
208 208
209void ScriptSystem::runScript(std::string mapName, std::string scriptName) { 209void ScriptSystem::loadMapScripts(std::string mapName) {
210 if (!loadedScripts_.count(mapName)) { 210 if (!loadedScripts_.count(mapName)) {
211 engine_.script_file("../res/scripts/" + mapName + ".lua"); 211 engine_.script_file("../res/scripts/" + mapName + ".lua");
212 loadedScripts_.insert(mapName); 212 loadedScripts_.insert(mapName);
213 } 213 }
214}
215
216bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) {
217 loadMapScripts(mapName);
218
219 return !!engine_.traverse_get<sol::function>(mapName, scriptName);
220}
221
222void ScriptSystem::runScript(std::string mapName, std::string scriptName) {
223 loadMapScripts(mapName);
214 224
215 Script newScript; 225 Script newScript;
216 newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); 226 newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state())));
217 newScript.callable.reset(new sol::coroutine(engine_.traverse_get<sol::function>(mapName, scriptName))); 227 newScript.callable.reset(new sol::coroutine(newScript.runner->state().traverse_get<sol::function>(mapName, scriptName)));
218 228
219 if (!*newScript.callable) { 229 if (!*newScript.callable) {
220 throw std::runtime_error("Error running script: " + mapName + "." + scriptName); 230 throw std::runtime_error("Error running script: " + mapName + "." + scriptName);