diff options
-rw-r--r-- | src/script_system.cpp | 38 | ||||
-rw-r--r-- | src/script_system.h | 8 |
2 files changed, 28 insertions, 18 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 | } |
diff --git a/src/script_system.h b/src/script_system.h index 2d099e3..18850b3 100644 --- a/src/script_system.h +++ b/src/script_system.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <sol/sol.hpp> | 4 | #include <sol/sol.hpp> |
5 | #include <memory> | 5 | #include <memory> |
6 | #include <set> | 6 | #include <set> |
7 | #include <list> | ||
7 | #include <string> | 8 | #include <string> |
8 | #include "system.h" | 9 | #include "system.h" |
9 | 10 | ||
@@ -24,9 +25,14 @@ public: | |||
24 | 25 | ||
25 | private: | 26 | private: |
26 | 27 | ||
28 | struct Script { | ||
29 | std::unique_ptr<sol::thread> runner; | ||
30 | std::unique_ptr<sol::coroutine> callable; | ||
31 | }; | ||
32 | |||
27 | Game& game_; | 33 | Game& game_; |
28 | sol::state engine_; | 34 | sol::state engine_; |
29 | std::unique_ptr<sol::coroutine> callable_; | 35 | std::list<Script> scripts_; |
30 | std::set<std::string> loadedScripts_; | 36 | std::set<std::string> loadedScripts_; |
31 | }; | 37 | }; |
32 | 38 | ||