summary refs log tree commit diff stats
path: root/src/script_system.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2021-07-05 11:58:29 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2021-07-05 11:58:29 -0400
commit324346a87c114f0991a092ac8653afecada364b5 (patch)
treef88ac730897056918b366e7fabaad2b136383d59 /src/script_system.cpp
parentd983ef6b8d66d916ed7502cf6d35b573c5366257 (diff)
downloadtanetane-324346a87c114f0991a092ac8653afecada364b5.tar.gz
tanetane-324346a87c114f0991a092ac8653afecada364b5.tar.bz2
tanetane-324346a87c114f0991a092ac8653afecada364b5.zip
Added background scripts
Background scripts are scripts that are launched when a map is loaded. They differ in use from a map's init script in that they are expected to contain an infinite loop. These scripts are linked to a sprite and will be killed when that sprite is destroyed (usually when the map is unloaded, but if the sprite is made persistent then it may last longer). The thread running the script is given no warning that it is being killed; the coroutine is simply never called again, and the thread is disposed of. Because of this, background scripts MUST ensure the game is in a consistent state before coroutine yielding, because it is not guaranteed that the coroutine will ever be called again.
Diffstat (limited to 'src/script_system.cpp')
-rw-r--r--src/script_system.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/script_system.cpp b/src/script_system.cpp index b4a7b9b..c820ecb 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp
@@ -269,6 +269,12 @@ void ScriptSystem::tick(double dt) {
269 }); 269 });
270} 270}
271 271
272void ScriptSystem::destroySprite(int spriteId) {
273 scripts_.remove_if([spriteId] (const Script& script) {
274 return (script.linkedSprite == spriteId);
275 });
276}
277
272void ScriptSystem::loadMapScripts(std::string mapName) { 278void ScriptSystem::loadMapScripts(std::string mapName) {
273 if (!loadedScripts_.count(mapName)) { 279 if (!loadedScripts_.count(mapName)) {
274 engine_.script_file("../res/scripts/" + mapName + ".lua"); 280 engine_.script_file("../res/scripts/" + mapName + ".lua");
@@ -282,7 +288,7 @@ bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) {
282 return !!engine_.traverse_get<sol::function>(mapName, scriptName); 288 return !!engine_.traverse_get<sol::function>(mapName, scriptName);
283} 289}
284 290
285void ScriptSystem::runScript(std::string mapName, std::string scriptName) { 291void ScriptSystem::runScript(std::string mapName, std::string scriptName, int linkedSprite) {
286 loadMapScripts(mapName); 292 loadMapScripts(mapName);
287 293
288 Script newScript; 294 Script newScript;
@@ -291,6 +297,7 @@ void ScriptSystem::runScript(std::string mapName, std::string scriptName) {
291#ifdef TANETANE_DEBUG 297#ifdef TANETANE_DEBUG
292 newScript.debugInfo = mapName + "." + scriptName; 298 newScript.debugInfo = mapName + "." + scriptName;
293#endif 299#endif
300 newScript.linkedSprite = linkedSprite;
294 301
295 if (!*newScript.callable) { 302 if (!*newScript.callable) {
296 throw std::runtime_error("Error running script: " + mapName + "." + scriptName); 303 throw std::runtime_error("Error running script: " + mapName + "." + scriptName);