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-21 23:46:37 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-21 23:46:37 -0500
commit9890cbf428b6216b05de02805c77c45d7b0983ab (patch)
tree71a26396e28dac90d64d13ae613e3e10cc7fdaab /src/script_system.cpp
parent36c784589714f4056eeeba7747e7c20a09333b70 (diff)
downloadtanetane-9890cbf428b6216b05de02805c77c45d7b0983ab.tar.gz
tanetane-9890cbf428b6216b05de02805c77c45d7b0983ab.tar.bz2
tanetane-9890cbf428b6216b05de02805c77c45d7b0983ab.zip
Multiple scripts can be run at once
And they're all on their own threads again. This is mostly so debug commands can be run during execution of other scripts. But it can also be useful for map init scripts (if we want a script to run whenever a map is loaded, even though map loading is usually done by a script).
Diffstat (limited to 'src/script_system.cpp')
-rw-r--r--src/script_system.cpp38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/script_system.cpp b/src/script_system.cpp index fcc7ab5..c7c3938 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp
@@ -192,17 +192,17 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
192void ScriptSystem::tick(double dt) { 192void ScriptSystem::tick(double dt) {
193 if (game_.isGameplayPaused()) return; 193 if (game_.isGameplayPaused()) return;
194 194
195 if (callable_ && *callable_) { 195 for (Script& script : scripts_) {
196 auto result = (*callable_)(dt); 196 auto result = (*script.callable)(dt);
197 if (!result.valid()) { 197 if (!result.valid()) {
198 sol::error e = result; 198 sol::error e = result;
199 throw e; 199 throw e;
200 } 200 }
201
202 if (!*callable_) {
203 callable_.reset();
204 }
205 } 201 }
202
203 scripts_.remove_if([] (const Script& script) {
204 return (!*script.callable);
205 });
206} 206}
207 207
208void ScriptSystem::runScript(std::string mapName, std::string scriptName) { 208void ScriptSystem::runScript(std::string mapName, std::string scriptName) {
@@ -211,41 +211,45 @@ void ScriptSystem::runScript(std::string mapName, std::string scriptName) {
211 loadedScripts_.insert(mapName); 211 loadedScripts_.insert(mapName);
212 } 212 }
213 213
214 callable_.reset(new sol::coroutine(engine_.traverse_get<sol::function>(mapName, scriptName))); 214 Script newScript;
215 newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state())));
216 newScript.callable.reset(new sol::coroutine(engine_.traverse_get<sol::function>(mapName, scriptName)));
215 217
216 if (!*callable_) { 218 if (!*newScript.callable) {
217 throw std::runtime_error("Error running script: " + mapName + "." + scriptName); 219 throw std::runtime_error("Error running script: " + mapName + "." + scriptName);
218 } 220 }
219 221
220 auto result = (*callable_)(); 222 auto result = (*newScript.callable)();
221 if (!result.valid()) { 223 if (!result.valid()) {
222 sol::error e = result; 224 sol::error e = result;
223 throw e; 225 throw e;
224 } 226 }
225 227
226 if (!*callable_) { 228 if (*newScript.callable) {
227 callable_.reset(); 229 scripts_.push_back(std::move(newScript));
228 } 230 }
229} 231}
230 232
231void ScriptSystem::runDebugScript(std::string script) { 233void ScriptSystem::runDebugScript(std::string script) {
232 sol::load_result loaded_script = engine_.load(script); 234 Script newScript;
235 newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state())));
233 236
234 callable_.reset(new sol::coroutine(loaded_script)); 237 sol::load_result loaded_script = newScript.runner->state().load(script);
238 newScript.callable.reset(new sol::coroutine(loaded_script));
235 239
236 if (!*callable_) { 240 if (!*newScript.callable) {
237 std::cout << "Error running debug command: " << script << std::endl; 241 std::cout << "Error running debug command: " << script << std::endl;
238 return; 242 return;
239 } 243 }
240 244
241 auto result = (*callable_)(); 245 auto result = (*newScript.callable)();
242 if (!result.valid()) { 246 if (!result.valid()) {
243 sol::error e = result; 247 sol::error e = result;
244 std::cout << e.what() << std::endl; 248 std::cout << e.what() << std::endl;
245 return; 249 return;
246 } 250 }
247 251
248 if (!*callable_) { 252 if (*newScript.callable) {
249 callable_.reset(); 253 scripts_.push_back(std::move(newScript));
250 } 254 }
251} 255}