Page 1 of 1

Problem with Custom Saver

Posted: Sat Mar 18, 2023 5:43 am
by eugeen
Hello!
I'm implementing my custom saver for Player's data that has to save a list of weapons and a dictionary with ammo.
Here's my code

Code: Select all

using System;
using UnityEngine;
using System.Collections.Generic;
using HarmonyLib;

namespace PixelCrushers
{


    public class PlayerSaver : Saver // Rename this class.
    {
        [Serializable]
        public class Data
        {
            public List<Weapon> weaponsInInventory;
            public Dictionary<AmmoType, int> ammunitionInInventory;
        }

        private ArmoryController armoryController;

        private Data _data = new Data();
  
        public override string RecordData()
        {
            
            armoryController = GetComponent<ArmoryController>();
            _data.weaponsInInventory = armoryController.weapons;
            _data.ammunitionInInventory = armoryController.ammoInventory.AsDictionary;
            print("ammo inventory count on saving: " + armoryController.ammoInventory.AsDictionary.Count);
            return SaveSystem.Serialize(_data);

            
        }

        public override void ApplyData(string s)
        {
            
            if (string.IsNullOrEmpty(s)) return; // No data to apply.
            Data data = SaveSystem.Deserialize<Data>(s,_data);
            if (data == null) return; // Serialized string isn't valid.

            _data = data;

            armoryController = GetComponent<ArmoryController>();

            armoryController.weapons = _data.weaponsInInventory;

            print("ammo inventory count on loading: " + _data.ammunitionInInventory.Count);
            for (int i = 0; i < _data.ammunitionInInventory.Count; i++)
            {
                armoryController.ammoInventory.Add((AmmoType)i, _data.ammunitionInInventory.GetValueSafe((AmmoType)i));
 
            }
            
        }

        public override void OnRestartGame()
        {
            
            Data data = new Data();
        }

    }

}

I use Save System and DiskSavedGameDataStorer. However, I noticed that the save file is empty after saving and I receive this error:

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
PixelCrushers.PlayerSaver.ApplyData (System.String s) (at Assets/Scripts/PlayerSaver.cs:69)
PixelCrushers.SaveSystem.ApplySavedGameData (PixelCrushers.SavedGameData savedGameData) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:738)
UnityEngine.Debug:LogException(Exception)
PixelCrushers.SaveSystem:ApplySavedGameData(SavedGameData) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:743)
PixelCrushers.<LoadSceneCoroutine>d__121:MoveNext() (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:867)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
I might have messed up some stuff in saver code but I don't really see any mistake so far.
Please help!

EDIT:
Ok, as I thought I messed up the saving code. I was trying to save a list of weapon MonoBehaviours that are referencing their WeaponData ScriptableObjects but apparently it doesn’t save this reference so instead now I’m saving a list of WeaponData directly and it works!

Re: Problem with Custom Saver

Posted: Sat Mar 18, 2023 8:28 am
by Tony Li
Hi,

Sounds like you got it all working now. By default, the saver uses JsonUtility serialization, so certain things such as Dictionary<> and references to assets aren't serializable.