diff options
Diffstat (limited to 'src/script_system.cpp')
-rw-r--r-- | src/script_system.cpp | 38 |
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) { | |||
192 | void ScriptSystem::tick(double dt) { | 192 | void 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 | ||
208 | void ScriptSystem::runScript(std::string mapName, std::string scriptName) { | 208 | void 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 | ||
231 | void ScriptSystem::runDebugScript(std::string script) { | 233 | void 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 | } |