Save System / Serialize & Deserialize List
Posted: Thu Aug 03, 2023 8:34 pm
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!
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;
}
}