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(); diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 592b769d6bd..3672a5d621c 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -70,7 +70,9 @@ 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(), + m_init_script_run_once() { add("Effect"); add("TextArray"); @@ -287,8 +289,10 @@ 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_once && !m_init_script_run) || !m_init_script_run_once)) { run_script(m_init_script, "init-script"); + m_init_script_run = true; } // Do not interpolate camera after it has been warped @@ -796,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 bcc603f07c3..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); @@ -260,6 +268,8 @@ 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; + 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();