Skip to content

Commit

Permalink
Allow saving init-script-run-once to level file
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbi committed Jul 14, 2024
1 parent 6fdf49f commit 45ee6bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/supertux/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Sector::Sector(Level& parent) :
m_gravity(10.0f),
m_collision_system(new CollisionSystem(*this)),
m_text_object(add<TextObject>("Text")),
m_init_script_run()
m_init_script_run(),
m_init_script_run_once()
{
add<DisplayEffect>("Effect");
add<TextArrayObject>("TextArray");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<GameObject*> objects(get_objects().size());
std::transform(get_objects().begin(), get_objects().end(), objects.begin(), [] (auto& obj) {
Expand Down
9 changes: 9 additions & 0 deletions src/supertux/sector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/supertux/sector_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sector*>(&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();
Expand Down

0 comments on commit 45ee6bd

Please sign in to comment.