Nothing has Change for My Bug

I did the same steps but i still have the same bug. Nothing has changed. Why?

Paste in your modified SavingWrapper and SavingSystem and we’ll take a look.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Saving;

namespace RPG.SceneManagement
{
public class SavingWrapper : MonoBehaviour
{

const string defaultSaveFile = "save";
[SerializeField] float fadeInTime=0.2f;
// listen key events
  private void Awake() 
        {
            StartCoroutine(LoadLastScene());
        }
    private IEnumerator LoadLastScene()
    {
        yield return GetComponent<SavingSystem>().LoadLastScene(defaultSaveFile);
        Fader fader=FindObjectOfType<Fader>();
        fader.FadeOutImmediate();
        yield return fader.FadeIn(fadeInTime);
    }
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.L))
        {
            Load();
        }
        if(Input.GetKeyDown(KeyCode.S))
        {
            Save();
        }
         if(Input.GetKeyDown(KeyCode.D))
        {
            Delete();
        }
    }
    public void Load()
    {
        GetComponent<SavingSystem>().Load(defaultSaveFile);
    }
     public void Save()
    {
        GetComponent<SavingSystem>().Save(defaultSaveFile);
    }
    private void Delete()
    {
        GetComponent<SavingSystem>().Delete(defaultSaveFile);
    }
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace GameDevTV.Saving
{
    /// <summary>
    /// This component provides the interface to the saving system. It provides
    /// methods to save and restore a scene.
    ///
    /// This component should be created once and shared between all subsequent scenes.
    /// </summary>
    public class SavingSystem : MonoBehaviour
    {
        /// <summary>
        /// Will load the last scene that was saved and restore the state. This
        /// must be run as a coroutine.
        /// </summary>
        /// <param name="saveFile">The save file to consult for loading.</param>
        public IEnumerator LoadLastScene(string saveFile)
        {
            Dictionary<string, object> state = LoadFile(saveFile);
            int buildIndex = SceneManager.GetActiveScene().buildIndex;
            if (state.ContainsKey("lastSceneBuildIndex"))
            {
                buildIndex = (int)state["lastSceneBuildIndex"];
            }
            yield return SceneManager.LoadSceneAsync(buildIndex);
            RestoreState(state);
        }

        /// <summary>
        /// Save the current scene to the provided save file.
        /// </summary>
        public void Save(string saveFile)
        {
            Dictionary<string, object> state = LoadFile(saveFile);
            CaptureState(state);
            SaveFile(saveFile, state);
        }

        /// <summary>
        /// Delete the state in the given save file.
        /// </summary>
        public void Delete(string saveFile)
        {
            File.Delete(GetPathFromSaveFile(saveFile));
        }

       

        public void Load(string saveFile)
        {
            RestoreState(LoadFile(saveFile));
        }
         // PRIVATE

        private Dictionary<string, object> LoadFile(string saveFile)
        {
            string path = GetPathFromSaveFile(saveFile);
            if (!File.Exists(path))
            {
                return new Dictionary<string, object>();
            }
            using (FileStream stream = File.Open(path, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return (Dictionary<string, object>)formatter.Deserialize(stream);
            }
        }

        private void SaveFile(string saveFile, object state)
        {
            string path = GetPathFromSaveFile(saveFile);
            print("Saving to " + path);
            using (FileStream stream = File.Open(path, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, state);
            }
        }

        private void CaptureState(Dictionary<string, object> state)
        {
            foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
            {
                state[saveable.GetUniqueIdentifier()] = saveable.CaptureState();
            }

            state["lastSceneBuildIndex"] = SceneManager.GetActiveScene().buildIndex;
        }

        private void RestoreState(Dictionary<string, object> state)
        {
            foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
            {
                string id = saveable.GetUniqueIdentifier();
                if (state.ContainsKey(id))
                {
                    saveable.RestoreState(state[id]);
                }
            }
        }

        private string GetPathFromSaveFile(string saveFile)
        {
            return Path.Combine(Application.persistentDataPath, saveFile + ".sav");
        }
    }
}

The scripts look correct. If you’re still experiencing the continuous load loop, then it’s likely that there is a SavingWrapper still in your scene. Check your scenes to make sure that when the game is not running, there is no PersistentObjectPrefab (it should be spawned in by the PersistentObjectSpawner in the Core, which will manage things to make sure there is only one loaded).

What happens when there is a SavingWrapper in the scene is that every time the scene is loaded, the LoadLastScene() coroutine fires when the SavingWrapper in the scene appears. Then the scene is loaded again, and a new SavingWrapper fires the LoadLastScene(), which continues forever.

Thank you for your quick reply. I make 2 test scenes. I tested the bug. There is nothing wrong. There is no persistent object prefab, saving wrapper or fader in my scene. It is spawning itself form Core like you said . Something in my scene causing this bug. I couldnt find it so i made a new scene, now everything is working smoothly . Thank you for your help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms