Page 1 of 1

Custom savers doesn't reset when restarting game

Posted: Wed Oct 25, 2023 10:53 am
by Fitbie
Hi, Tony!
I've got some custom savers. For example this one.
Spoiler

Code: Select all

    public class InventorySaver : Saver
    {
        [Serializable]
        public class Data //Contains data for serializing
        {
            public InvItem[] items = new InvItem[6];
            public InvItem[] itemsMare = new InvItem[6];
        }

        //Arrays to operate with
        private InvItem[] itemsArray;
        private InvItem[] itemsMareArray;
        
        public override void Start() 
        {
            itemsArray = GameManager.Instance.inventory.items;
            itemsMareArray = GameManager.Instance.inventory.itemsMare;
        }

        public override string RecordData()
        {
            Data data = new Data();
            
            Array.Copy(itemsArray, data.items, itemsArray.Length); 
            Array.Copy(itemsMareArray, data.itemsMare, itemsMareArray.Length);
        
            return SaveSystem.Serialize(data);
        }
    
        public override void ApplyData(string s)
        {
            Data data = (Data)SaveSystem.Deserialize<Data>(s);
            if (data == null) return;

            Array.Copy(data.items, itemsArray, itemsArray.Length);
            Array.Copy(data.itemsMare, itemsMareArray, itemsMareArray.Length);
        }
        
    }
The problem is that if I go to the main menu and start a new game (which will call SaveSystem.RestartGame()) - then all the data will be reset, except for the data of these savers. And if I exit app and then open it and start new game- everything is ok.
I should mention that when I exit to the main menu, all objects are destroyed, so all the savers in the main menu are "clean". Apparently the data itself remains for some reason.
Any idea? Thanks!

Re: Custom savers doesn't reset when restarting game

Posted: Wed Oct 25, 2023 11:10 am
by Tony Li
Hi,

Add an OnRestartGame() method:

Code: Select all

public override void OnRestartGame()
{
    ...
}

Re: Custom savers doesn't reset when restarting game

Posted: Wed Oct 25, 2023 11:30 am
by Fitbie
Yeah, i know about that, but empty method doesn't fix it.
How can i reset data itself? Thanks!

Re: Custom savers doesn't reset when restarting game

Posted: Wed Oct 25, 2023 11:34 am
by Tony Li
Add your code to reset the data in place of "...".