Custom savers doesn't reset when restarting game

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Fitbie
Posts: 44
Joined: Tue Dec 07, 2021 6:30 pm

Custom savers doesn't reset when restarting game

Post 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!
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom savers doesn't reset when restarting game

Post by Tony Li »

Hi,

Add an OnRestartGame() method:

Code: Select all

public override void OnRestartGame()
{
    ...
}
Fitbie
Posts: 44
Joined: Tue Dec 07, 2021 6:30 pm

Re: Custom savers doesn't reset when restarting game

Post by Fitbie »

Yeah, i know about that, but empty method doesn't fix it.
How can i reset data itself? Thanks!
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom savers doesn't reset when restarting game

Post by Tony Li »

Add your code to reset the data in place of "...".
Post Reply