Page 1 of 1

Save System / Serialize & Deserialize List

Posted: Thu Aug 03, 2023 8:34 pm
by DrFillangee
I am trying to add save functionality to a custom class that stores a 2D array for a grid of objects. Since you cannot serialize a 2D array I convert this array into a List SeedData that holds the 2D array points as well as what the object contains (a custom Item class and 2 ints for tracking time). Currently, when Deserializing the List, it is returning a List of 0.
I tested using some Debugs and confirmed that it is serializing a List of 16 objects when saving but when I reload the scene and check the Deserialized List (seedDataList) count it is showing 0 but it doesn't return Null. Below is my code, is there something I might be missing as to why I cannot Deserialize the List? I also included the Custom class SeedData for reference and I am using the JSON Data Serializer.
Any help is appreciated, thank you!

Code: Select all

public override string RecordData()
        {
            var seedDataList = new List<SeedData>();
            
            for (int row = 0; row < gridRows; row++)
            {
                for (int col = 0; col < gridColumns; col++)
                {
                    var seedData = new SeedData(_plantableAreas[row, col].ItemSeed, 
                        _plantableAreas[row, col].MomentsToDate, _plantableAreas[row, col].GrowthIndex, row, col);
                    seedDataList.Add(seedData);
                }
            }
            
            return SaveSystem.Serialize(seedDataList);
        }

        public override void ApplyData(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                Debug.Log("<color=Red>No Seed Data to load</color>");
                return;
            }
            
            var seedDataList = SaveSystem.Deserialize<List<SeedData>>(data);
            foreach (var seedData in seedDataList)
            {
                _plantableAreas[seedData.Row, seedData.Col].LoadSeedData(seedData);
                Debug.Log($"{_plantableAreas[seedData.Row, seedData.Col].name} Data added.");
            }
        }

        [System.Serializable]
        public class SeedData
        {
            public ItemSeed Seed;
            public int MomentsToDate;
            public int GrowthIndex;
            public int Row;
            public int Col;

            public SeedData(ItemSeed seed, int momentsToDate, int growthIndex, int row, int col)
            {
                Seed = seed;
                MomentsToDate = momentsToDate;
                GrowthIndex = growthIndex;
                Row = row;
                Col = col;
            }
        }

Re: Save System / Serialize & Deserialize List

Posted: Thu Aug 03, 2023 8:53 pm
by Tony Li
Hi,

By default, the Save System uses JsonDataSerializer, which uses Unity's JsonUtility. JsonUtility is very fast, but it has some limitations -- one of which is that it can't serialize a List<T> by itself. You must put it in a class or struct, like:

Code: Select all

[System.Serializable]
public class Data
{
    public List<SeedData> list;
    public int gridRows; // Maybe you want to record gridRows in case it's variable.
}

        public override string RecordData()
        {
            var seedDataList = new List<SeedData>();
            
            for (int row = 0; row < gridRows; row++)
            {
                for (int col = 0; col < gridColumns; col++)
                {
                    var seedData = new SeedData(_plantableAreas[row, col].ItemSeed, 
                        _plantableAreas[row, col].MomentsToDate, _plantableAreas[row, col].GrowthIndex, row, col);
                    seedDataList.Add(seedData);
                }
            }
        
            var data = new Data();
            data.list = seedDataList;    
            data.gridRows = gridRows;
            return SaveSystem.Serialize(data);
        }

Re: Save System / Serialize & Deserialize List

Posted: Thu Aug 03, 2023 10:10 pm
by DrFillangee
That did the trick! I didn't realize Unity's JsonUtility had that restriction. Thank you so much for the quick reply!

Re: Save System / Serialize & Deserialize List

Posted: Thu Aug 03, 2023 10:25 pm
by Tony Li
Glad to help!