File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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" ) ;
You can’t perform that action at this time.
0 commit comments