From eb3fece89703baef462ac2cf4111f9a1bd04ce54 Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Wed, 5 Nov 2025 20:55:09 +0100 Subject: [PATCH] The forgotten push --- Assets/Scripts/Core/SceneManagerService.cs | 7 +++++ Assets/Scripts/PuzzleS/PuzzleManager.cs | 32 +++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Core/SceneManagerService.cs b/Assets/Scripts/Core/SceneManagerService.cs index 70c9c4df..423c36ff 100644 --- a/Assets/Scripts/Core/SceneManagerService.cs +++ b/Assets/Scripts/Core/SceneManagerService.cs @@ -324,6 +324,13 @@ namespace Core { LogDebugMessage($"Skipping save for: {oldSceneName} (skipSave=true)"); } + + // PHASE 4: Clear PuzzleManager state before scene transition + if (PuzzleS.PuzzleManager.Instance != null) + { + LogDebugMessage($"Clearing puzzle state before scene transition"); + PuzzleS.PuzzleManager.Instance.ClearPuzzleState(); + } // PHASE 5: Remove all AstarPath (A* Pathfinder) singletons before loading the new scene var astarPaths = FindObjectsByType(FindObjectsSortMode.None); diff --git a/Assets/Scripts/PuzzleS/PuzzleManager.cs b/Assets/Scripts/PuzzleS/PuzzleManager.cs index da51e85e..12ac0610 100644 --- a/Assets/Scripts/PuzzleS/PuzzleManager.cs +++ b/Assets/Scripts/PuzzleS/PuzzleManager.cs @@ -184,6 +184,8 @@ namespace PuzzleS return; } + // Reset restoration flag when loading new scene data + _isDataRestored = false; _isDataLoaded = false; string addressablePath = $"Puzzles/{currentScene}"; @@ -213,11 +215,10 @@ namespace PuzzleS _currentLevelData = handle.Result; Logging.Debug($"[Puzzles] Loaded level data: {_currentLevelData.levelId} with {_currentLevelData.allSteps.Count} steps"); - // Reset state - _completedSteps.Clear(); - _unlockedSteps.Clear(); + // Don't clear steps here - SceneManagerService calls ClearPuzzleState() before scene transitions + // This allows save restoration to work properly without race conditions - // Unlock initial steps + // Unlock initial steps (adds to existing unlocked steps from save restoration) UnlockInitialSteps(); // Update all registered behaviors now that data is loaded @@ -567,6 +568,27 @@ namespace PuzzleS return _isDataLoaded; } + /// + /// Clears all puzzle state (completed steps, unlocked steps, registrations). + /// Called by SceneManagerService before scene transitions to ensure clean state. + /// + public void ClearPuzzleState() + { + Logging.Debug("[PuzzleManager] Clearing puzzle state"); + + _completedSteps.Clear(); + _unlockedSteps.Clear(); + _isDataRestored = false; + + // Clear any pending registrations from the old scene + _pendingRegistrations.Clear(); + _pendingUnlocks.Clear(); + + // Unregister all step behaviours from the old scene + _stepBehaviours.Clear(); + _registeredBehaviours.Clear(); + } + #region Save/Load Lifecycle Hooks protected override string OnSceneSaveRequested() @@ -591,6 +613,8 @@ namespace PuzzleS protected override void OnSceneRestoreRequested(string data) { + Debug.Log("[XAXA] PuzzleManager loading with data: " + data); + if (string.IsNullOrEmpty(data) || data == "{}") { Logging.Debug("[PuzzleManager] No puzzle save data to restore");