From 6fdf49f1970daccda35dc9eb11ad94887a669703 Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Thu, 11 Jul 2024 10:56:56 +0200 Subject: [PATCH 1/3] Run init-script only once after a sector has loaded Fixes #698 --- src/supertux/sector.cpp | 6 ++++-- src/supertux/sector.hpp | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 592b769d6bd..1f59723fcfe 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -70,7 +70,8 @@ Sector::Sector(Level& parent) : m_foremost_opaque_layer(), m_gravity(10.0f), m_collision_system(new CollisionSystem(*this)), - m_text_object(add("Text")) + m_text_object(add("Text")), + m_init_script_run() { add("Effect"); add("TextArray"); @@ -287,8 +288,9 @@ Sector::activate(const Vector& player_pos) } // Run init script - if (!m_init_script.empty() && !Editor::is_active()) { + if (!m_init_script.empty() && !Editor::is_active() && !m_init_script_run) { run_script(m_init_script, "init-script"); + m_init_script_run = true; } // Do not interpolate camera after it has been warped diff --git a/src/supertux/sector.hpp b/src/supertux/sector.hpp index bcc603f07c3..6dc9dcd47a7 100644 --- a/src/supertux/sector.hpp +++ b/src/supertux/sector.hpp @@ -260,6 +260,7 @@ class Sector final : public Base::Sector Vector m_last_translation; // For camera interpolation at high frame rates float m_last_scale; float m_last_dt; + bool m_init_script_run; private: Sector(const Sector&) = delete; From 45ee6bd1d2f6f8cf59deb34bb689252faf352ccd Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Sun, 14 Jul 2024 21:00:45 +0200 Subject: [PATCH 2/3] Allow saving init-script-run-once to level file --- src/supertux/sector.cpp | 10 ++++++++-- src/supertux/sector.hpp | 9 +++++++++ src/supertux/sector_parser.cpp | 8 ++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 1f59723fcfe..3672a5d621c 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -71,7 +71,8 @@ Sector::Sector(Level& parent) : m_gravity(10.0f), m_collision_system(new CollisionSystem(*this)), m_text_object(add("Text")), - m_init_script_run() + m_init_script_run(), + m_init_script_run_once() { add("Effect"); add("TextArray"); @@ -288,7 +289,8 @@ Sector::activate(const Vector& player_pos) } // Run init script - if (!m_init_script.empty() && !Editor::is_active() && !m_init_script_run) { + if (!m_init_script.empty() && !Editor::is_active() && + ((m_init_script_run_once && !m_init_script_run) || !m_init_script_run_once)) { run_script(m_init_script, "init-script"); m_init_script_run = true; } @@ -798,6 +800,10 @@ Sector::save(Writer &writer) writer.write("init-script", m_init_script,false); } + if (m_init_script_run_once) { + writer.write("init-script-run-once", m_init_script_run_once); + } + // saving objects; std::vector objects(get_objects().size()); std::transform(get_objects().begin(), get_objects().end(), objects.begin(), [] (auto& obj) { diff --git a/src/supertux/sector.hpp b/src/supertux/sector.hpp index 6dc9dcd47a7..00504cfc733 100644 --- a/src/supertux/sector.hpp +++ b/src/supertux/sector.hpp @@ -85,6 +85,14 @@ class Sector final : public Base::Sector Level& get_level() const { return m_level; } TileSet* get_tileset() const override; bool in_worldmap() const override; + void set_init_script_run_once(bool run_once) + { + m_init_script_run_once = run_once; + } + bool get_init_script_run_once() const + { + return m_init_script_run_once; + } /** activates this sector (change music, initialize player class, ...) */ void activate(const std::string& spawnpoint); @@ -261,6 +269,7 @@ class Sector final : public Base::Sector float m_last_scale; float m_last_dt; bool m_init_script_run; + bool m_init_script_run_once; private: Sector(const Sector&) = delete; diff --git a/src/supertux/sector_parser.cpp b/src/supertux/sector_parser.cpp index 43fb63190fe..65d4608fe5a 100644 --- a/src/supertux/sector_parser.cpp +++ b/src/supertux/sector_parser.cpp @@ -142,6 +142,14 @@ SectorParser::parse(const ReaderMapping& reader) iter.get(value); m_sector.set_init_script(value); } + else if(iter.get_key() == "init-script-run-once") + { + auto sector = dynamic_cast(&m_sector); + if (!sector) continue; + bool value; + iter.get(value); + sector->set_init_script_run_once(value); + } else if (iter.get_key() == "ambient-light") { const auto& sx = iter.get_sexp(); From e7ca5c24f6469209a5017b8821e814162b68c7de Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Sun, 14 Jul 2024 21:10:38 +0200 Subject: [PATCH 3/3] Add toggle to editor sector menu --- src/supertux/menu/editor_sector_menu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/supertux/menu/editor_sector_menu.cpp b/src/supertux/menu/editor_sector_menu.cpp index 2813334a02f..942d080ac79 100644 --- a/src/supertux/menu/editor_sector_menu.cpp +++ b/src/supertux/menu/editor_sector_menu.cpp @@ -35,6 +35,7 @@ EditorSectorMenu::EditorSectorMenu() : add_hl(); add_textfield(_("Name"), §or->m_name); add_script(_("Initialization script"), §or->m_init_script); + add_toggle(0, _("Run initialization script only once"), §or->m_init_script_run_once); add_floatfield(_("Gravity"), §or->m_gravity); add_hl();