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/game.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index 80ff506..dff8af0 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -52,6 +52,7 @@ void Game::loadMap(std::string filename) { // Load the new map. map_ = std::make_unique(filename); + std::vector> backgroundScripts; for (const Prototype& p : map_->getPrototypes()) { if (spritesByAlias_.count(p.name)) continue; @@ -84,6 +85,9 @@ void Game::loadMap(std::string filename) { getSprite(spriteId).mirrorAxis = p.mirrorAxis; getSprite(spriteId).mirroredSpriteId = getSpriteByAlias(p.spriteToMirror); } + if (!p.backgroundScript.empty()) { + backgroundScripts.push_back({spriteId, p.backgroundScript}); + } } for (const Trigger& t : map_->getTriggers()) { @@ -99,4 +103,9 @@ void Game::loadMap(std::string filename) { if (getSystem().mapHasScript(map_->getName(), "init")) { getSystem().runScript(map_->getName(), "init"); } + + // Run any background scripts associated with new sprites. + for (auto& [spriteId, scriptName] : backgroundScripts) { + getSystem().runScript(map_->getName(), scriptName, spriteId); + } } -- cgit 1.4.1