Skip to content

Commit 9a168ca

Browse files
Fixes #444 Invalidate UI state/windows when a game starts
1 parent 946b252 commit 9a168ca

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

Pulsar4X/Pulsar4X.Client/Interface/Menus/LoadGame.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal void LoadFile(string filenamepath)
4848
string contents = File.ReadAllText(filenamepath);
4949
var loadedGame = Game.Load(contents);
5050

51+
_uiState.ClearGameState();
5152
_uiState.Game = loadedGame;
5253

5354
// TODO: need to figure out a way to properly handle this

Pulsar4X/Pulsar4X.Client/Interface/Menus/NewGameMenu.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ void CreateNewGame()
528528
// Call the game post new game initialization
529529
game.PostNewGameInitialization();
530530

531+
_uiState.ClearGameState();
531532
_uiState.Game = game;
532533
_uiState.SetFaction(playerFaction, true);
533534
_uiState.SetActiveSystem(startingSystem.ManagerID);
@@ -746,6 +747,7 @@ public static void QuickstartGame()
746747
// Initialize game
747748
game.PostNewGameInitialization();
748749

750+
_uiState.ClearGameState();
749751
_uiState.Game = game;
750752
_uiState.SetFaction(playerFaction, true);
751753
_uiState.SetActiveSystem(startingSystem.ManagerID);

Pulsar4X/Pulsar4X.Client/State/GlobalUIState.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,27 @@ private void DeactivateAllClosableWindows()
188188
}
189189
}
190190

191+
/// <summary>
192+
/// Clears all cached UI state to prepare for a new game.
193+
/// This must be called before setting up a new game to prevent stale
194+
/// references from the previous game
195+
/// </summary>
196+
internal void ClearGameState()
197+
{
198+
LoadedWindows.Clear();
199+
LoadedNonUniqueWindows.Clear();
200+
EntityWindows.Clear();
201+
StarSystemStates.Clear();
202+
LastClickedEntity = null;
203+
PrimaryEntity = null;
204+
Faction = null;
205+
PlayerFaction = null;
206+
SelectedStarSystemId = "";
207+
_lastContextMenuOpenedEntityGuid = -1;
208+
ContextMenu = null;
209+
ActiveWindow = null;
210+
}
211+
191212
internal void SetFaction(Entity factionEntity, bool setAsPlayer = false)
192213
{
193214
if(Game == null) throw new NullReferenceException("Game is null");

0 commit comments

Comments
 (0)