Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run init-script only once after a sector has loaded #3011

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/supertux/menu/editor_sector_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ EditorSectorMenu::EditorSectorMenu() :
add_hl();
add_textfield(_("Name"), &sector->m_name);
add_script(_("Initialization script"), &sector->m_init_script);
add_toggle(0, _("Run initialization script only once"), &sector->m_init_script_run_once);
add_floatfield(_("Gravity"), &sector->m_gravity);

add_hl();
Expand Down
12 changes: 10 additions & 2 deletions src/supertux/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextObject>("Text"))
m_text_object(add<TextObject>("Text")),
m_init_script_run(),
m_init_script_run_once()
{
add<DisplayEffect>("Effect");
add<TextArrayObject>("TextArray");
Expand Down Expand Up @@ -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)) {
Copy link
Member

@mrkubax10 mrkubax10 Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose second line could be replaced with (!m_init_script_run_once || !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
Expand Down Expand Up @@ -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<GameObject*> objects(get_objects().size());
std::transform(get_objects().begin(), get_objects().end(), objects.begin(), [] (auto& obj) {
Expand Down
10 changes: 10 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 @@ -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;
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
Loading