From b25daba56117a01621ce6718b053a2d65c7809dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guilloux?= Date: Thu, 3 Apr 2025 21:14:45 +0200 Subject: [PATCH] Fix: [Script] Reset instance when changing running scripts in scenario editor (#13906) --- src/game/game.hpp | 5 +++++ src/game/game_core.cpp | 14 ++++++++------ src/script/script_gui.cpp | 13 +++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/game/game.hpp b/src/game/game.hpp index 036ebfe44a..d6e8ba65c5 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -95,6 +95,11 @@ public: */ static class GameInstance *GetInstance() { return Game::instance.get(); } + /** + * Reset the current active instance. + */ + static void ResetInstance(); + /** Wrapper function for GameScanner::HasGame */ static bool HasGame(const struct ContentInfo *ci, bool md5sum); static bool HasGameLibrary(const ContentInfo *ci, bool md5sum); diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index 0542a42fea..dbd1b672c8 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -103,8 +103,7 @@ { Backup cur_company(_current_company); - Game::instance.reset(); - Game::info = nullptr; + Game::ResetInstance(); cur_company.Restore(); @@ -162,10 +161,7 @@ if (!_settings_game.script_config.game->ResetInfo(true)) { Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_game.script_config.game->GetName()); _settings_game.script_config.game->Change(std::nullopt); - if (Game::instance != nullptr) { - Game::instance.reset(); - Game::info = nullptr; - } + if (Game::instance != nullptr) Game::ResetInstance(); } else if (Game::instance != nullptr) { Game::info = _settings_game.script_config.game->GetInfo(); } @@ -234,6 +230,12 @@ return Game::scanner_library->FindLibrary(library, version); } +/* static */ void Game::ResetInstance() +{ + Game::instance.reset(); + Game::info = nullptr; +} + /** * Check whether we have an Game (library) with the exact characteristics as ci. * @param ci the characteristics to search on (shortname and md5sum) diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index b2a4930581..28c9f2f379 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -173,8 +173,21 @@ struct ScriptListWindow : public Window { std::advance(it, this->selected); GetConfig(slot)->Change(it->second->GetName(), it->second->GetVersion()); } + if (_game_mode == GM_EDITOR) { + if (slot == OWNER_DEITY) { + if (Game::GetInstance() != nullptr) Game::ResetInstance(); + Game::StartNew(); + } else { + Company *c = Company::GetIfValid(slot); + if (c != nullptr && c->ai_instance != nullptr) { + c->ai_instance.reset(); + AI::StartNew(slot); + } + } + } InvalidateWindowData(WC_GAME_OPTIONS, slot == OWNER_DEITY ? WN_GAME_OPTIONS_GS : WN_GAME_OPTIONS_AI); InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); + InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); CloseWindowByClass(WC_QUERY_STRING); InvalidateWindowClassesData(WC_TEXTFILE); }