From 324346a87c114f0991a092ac8653afecada364b5 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 5 Jul 2021 11:58:29 -0400 Subject: 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. --- src/script_system.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/script_system.cpp') 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) { }); } +void ScriptSystem::destroySprite(int spriteId) { + scripts_.remove_if([spriteId] (const Script& script) { + return (script.linkedSprite == spriteId); + }); +} + void ScriptSystem::loadMapScripts(std::string mapName) { if (!loadedScripts_.count(mapName)) { engine_.script_file("../res/scripts/" + mapName + ".lua"); @@ -282,7 +288,7 @@ bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) { return !!engine_.traverse_get(mapName, scriptName); } -void ScriptSystem::runScript(std::string mapName, std::string scriptName) { +void ScriptSystem::runScript(std::string mapName, std::string scriptName, int linkedSprite) { loadMapScripts(mapName); Script newScript; @@ -291,6 +297,7 @@ void ScriptSystem::runScript(std::string mapName, std::string scriptName) { #ifdef TANETANE_DEBUG newScript.debugInfo = mapName + "." + scriptName; #endif + newScript.linkedSprite = linkedSprite; if (!*newScript.callable) { throw std::runtime_error("Error running script: " + mapName + "." + scriptName); -- cgit 1.4.1