Help/ advice in Game clashing

Guys I have a problem, I need help with a bug Don’t know what’s causing it .
When the game plays, random audio plays and after the audio finishes, the game either crashes or restarts the level

Hi,

In which course and lecture are you?

Inventory integration, it’s the save system that’s making the game crash, or to be specific. The saving wrappper

I have no idea where the random audio would be coming from…
It occurs to me you might be taking the Inventory course without the RPG course, so I’ll put an explainer here:

Here’s what needs to happen with the SavingWrapper/SavingSystem…

  1. A GameObject containg the Saving Wrapper and Saving System should be put in a prefab called “PersistentObjects”
  2. You should have this script on an item that is loaded in every game scene:
    public class PersistentObjectSpawner : MonoBehaviour
    {
        [SerializeField] GameObject persistentObjectPrefab;

        static bool hasSpawned = false;

        private void Awake()
        {
            if (hasSpawned) return;

            SpawnPersistentObjects();

            hasSpawned = true;
        }

        private void SpawnPersistentObjects()
        {
            GameObject persistentObject = Instantiate(persistentObjectPrefab);
            DontDestroyOnLoad(persistentObject);
        }
    }

Link the PersistentObjects prefab to this script.
Do not put either the SavingSystem or SavingWrapper component in any scene object!

You can stop the automatic loading of scenes by removing the Awake() method in SavingWrapper.cs
If your Saving and Loading is crashing with a Serialization error, this is usually after changing the return type in something you are saving/restoring. You’ll need to delete the file. The easiest way to do this is to press the Delete key while the game is running, and the SavingWrapper will delete the file.

1 Like

Thank you ,thank you

You saved me , for real. Thank you I’m really grateful

For a refresher on using the Saving System, review the lectures in the Inventory Course in the categery Saving System Overview. https://www.gamedev.tv/courses/734254/lectures/13358416

The real teardown of how to get the most out of the system is in the RPG course.

1 Like

I will make sure to get it and check it out.

I have a problem, the save function is not being called. I have tried changing buttons. Calling the function directly , adding debug statements. None of them are returning on the save function. Only the load and delete are working

Which function are you calling to Save? (It should be the Save() in SavingWrapper). Describe the method you are using to call the function.

The method is called save ()
Then inside the method. You get the component of the savingsystem.save(defaults save);
That’s where the debug. Log calls lastly.

I also added a debug .log in the save method in the saving system. But it wasn’t called

Are you using the one in SavingWrapper or your own?

I used the wrapper directly without changing anything. Oh only removing the awake method. Which had a coroutine

I know you mentioned you put in Debugs, but I’m going to layout a series of Debugs together in the hopes I can see what’s going on. (You’ve mentioned before you won’t upload the project, so my options on this are limited).

In SavingWrapper.cs:

        public void Save()
        {
            SavingSystem system = GetComponent<SavingSystem>();
            if (system == null)
            {
                Debug.Log("No Saving System was found");
                return;
            }
            else
            {
                Debug.Log($"Saving System located. {system}");
            }
            GetComponent<SavingSystem>().Save(defaultSaveFile);
        }

In SavingSystem.cs

        public void Save(string saveFile)
        {
            Debug.Log($"SavingSystem.Save({saveFile}");
            Dictionary<string, object> state = LoadFile(saveFile);
            CaptureState(state);
            SaveFile(saveFile, state);
        }

        private void CaptureState(Dictionary<string, object> state)
        {
            Debug.Log($"Capturing State, {FindObjectsOfType<SaveableEntity>().Length} Saveable Entites found.");
            foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
            {
                Debug.Log($"Found {saveable}, ID={saveable.GetUniqueIdentifier()}");
                state[saveable.GetUniqueIdentifier()] = saveable.CaptureState();
            }

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

And finally in SaveableEntity

        public object CaptureState()
        {
            Debug.Log($"{name} ({GetUniqueIdentifier()}) is capturing it's state {GetComponents<ISaveable>().Length} components found.");
            Dictionary<string, object> state = new Dictionary<string, object>();
            foreach (ISaveable saveable in GetComponents<ISaveable>())
            {
                Debug.Log($"{name} ({GetUniqueIdentifier()}) -> {saveable.GetType().ToString()}");
                state[saveable.GetType().ToString()] = saveable.CaptureState();
            }
            return state;
        }

Oh on this , I made an empty scene and only included the player with needed scripts and gaveadding the saveable entity to it. And capturing the states his transform following the tutorial.
Alright will add the statements to it.

If capturing his state yields the transforms, then it would appear that save is working properly.

Saving System located. Save System(Clone) (GameDevTV.Saving.SavingSystem)
UnityEngine.Debug:Log(Object)
InventoryExample.SceneManagement.SavingWrapper:Save() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:53)
InventoryExample.SceneManagement.SavingWrapper:Update() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:23)

SavingSystem.Save(save
UnityEngine.Debug:Log(Object)
GameDevTV.Saving.SavingSystem:Save(String) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:42)
InventoryExample.SceneManagement.SavingWrapper:Save() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:55)
InventoryExample.SceneManagement.SavingWrapper:Update() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:23)

Capturing State, 2 Saveable Entites found.
UnityEngine.Debug:Log(Object)
GameDevTV.Saving.SavingSystem:CaptureState(Dictionary`2) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:90)
GameDevTV.Saving.SavingSystem:Save(String) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:44)
InventoryExample.SceneManagement.SavingWrapper:Save() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:55)
InventoryExample.SceneManagement.SavingWrapper:Update() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:23)

Found Girl_01_g Variant (GameDevTV.Saving.SaveableEntity), ID=ca139e40-289b-49a0-9248-16b9d97665a5
UnityEngine.Debug:Log(Object)
GameDevTV.Saving.SavingSystem:CaptureState(Dictionary`2) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:93)
GameDevTV.Saving.SavingSystem:Save(String) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:44)
InventoryExample.SceneManagement.SavingWrapper:Save() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:55)
InventoryExample.SceneManagement.SavingWrapper:Update() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:23)

Girl_01_g Variant (ca139e40-289b-49a0-9248-16b9d97665a5) is capturing it’s state 9 components found.
UnityEngine.Debug:Log(Object)
GameDevTV.Saving.SaveableEntity:CaptureState() (at Assets/GameDev.tv Assets/Scripts/Saving/SaveableEntity.cs:42)
GameDevTV.Saving.SavingSystem:CaptureState(Dictionary`2) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:94)
GameDevTV.Saving.SavingSystem:Save(String) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:44)
InventoryExample.SceneManagement.SavingWrapper:Save() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:55)
InventoryExample.SceneManagement.SavingWrapper:Update() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:23)

Girl_01_g Variant (ca139e40-289b-49a0-9248-16b9d97665a5) -> PlayerCombat
UnityEngine.Debug:Log(Object)
GameDevTV.Saving.SaveableEntity:CaptureState() (at Assets/GameDev.tv Assets/Scripts/Saving/SaveableEntity.cs:46)
GameDevTV.Saving.SavingSystem:CaptureState(Dictionary`2) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:94)
GameDevTV.Saving.SavingSystem:Save(String) (at Assets/GameDev.tv Assets/Scripts/Saving/SavingSystem.cs:44)
InventoryExample.SceneManagement.SavingWrapper:Save() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:55)
InventoryExample.SceneManagement.SavingWrapper:Update() (at Assets/Scripts/SceneManagement/SavingWrapper.cs:23)

Does it mean, it’s not finding the transform. Or it’s something else?.

I followed the video in adding the player transform to the capture state and restore state. So where am I going wrong ?.

What this means is that hte save system itself is saving data.

To save the player’s location, one of the components on the ISaveable needs to capture and restore the location. In our RPG game, that’s the Mover.cs that is responsible for this. Be sure to review the video here: https://www.gamedev.tv/courses/734254/lectures/13358416, at around the 9 minute mark Sam explains how to capture and restore the player’s location.