Saving List of Custom Class within a Custom Class

Discussion and help for Easy Save 3
Post Reply
cptscrimshaw
Posts: 5
Joined: Wed Mar 31, 2021 11:19 pm

Saving List of Custom Class within a Custom Class

Post by cptscrimshaw »

I have two custom classes. The first contains a list of the second custom class. It appears to be saving correctly, but whenever I load the data, the base class loads correctly, but the data from the sub class doesn't populate the list. I'm wondering if I'm missing something, or if I need to save each list separately. Here is the code for both classes, the code I'm using to save, and the save file data:

Base Class:

Code: Select all

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

[System.Serializable]
public class TavernTable : MonoBehaviour
{
    [Header("Basic Info")]
    public int slot;
    public string faction;
    public List<TavernSeat> tavernSeats = new List<TavernSeat>();

    //[Header("Graphics")]

    public TavernTable()
    {

    }

    public TavernTable(int slot, string faction, List<TavernSeat> tavernSeats)
    {
        this.slot = slot;
        this.faction = faction;
        this.tavernSeats = tavernSeats;
    }
}
Sub Class:

Code: Select all

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

[System.Serializable]
public class TavernSeat : MonoBehaviour
{
    [Header("Basic Info")]
    public string faction;
    public string npcString;
    public string dish;
    public bool occupied;

    public string facing;

    [Header("Graphics")]
    public Sprite dishSprite;
    public Animator dishAnimation;

    public TavernSeat()
    {

    }

    public TavernSeat(string faction, string npcString, string dish, bool occupied, string facing)
    {
        this.faction = faction;
        this.npcString = npcString;
        this.dish = dish;
        this.occupied = occupied;
        this.facing = facing;
    }
}
Saving/Loading:

Code: Select all

[SerializeField] private List<TavernTable> tavernTables = new List<TavernTable>();

    private void Awake()
    {
        SaveSystem.saveDataApplied += OnSaveDataApplied;
        EventHandler.ScenePortalTriggered += SaveBeforeChangingScenes;

        if (Instance != null && Instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            Instance = this;
        }
    }

    public void OnSaveDataApplied()
    {
        if (!ManualSave.manuallyLoadingGame)
        {
            if (ES3.KeyExists("tavernTables"))
            {
                tavernTables = ES3.Load<List<TavernTable>>("tavernTables");
            }
        }
    }

    void SaveBeforeChangingScenes()
    {
        ES3.Save("tavernTables", tavernTables);
    }

    public void OnApplicationQuit()
    {
        ES3.Save("tavernTables", tavernTables);
    }

    private void OnDestroy()
    {
        SaveSystem.saveDataApplied -= OnSaveDataApplied;
        EventHandler.ScenePortalTriggered -= SaveBeforeChangingScenes;
    }

Code: Select all

"tavernTables" : {
		"__type" : "System.Collections.Generic.List`1[[TavernTable, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]],mscorlib",
		"value" : [
			{
				"_ES3Ref" : "5414689953241649868",
				"goID" : "6033350127038508259",
				"slot" : 0,
				"faction" : "Merchant Guild",
				"tavernSeats" : [
					{
						"_ES3Ref" : "3685158514958316190"
					},{
						"_ES3Ref" : "1220786584282548631"
					},{
						"_ES3Ref" : "6747607265961300836"
					},{
						"_ES3Ref" : "5799960329459964387"
					}
				]
			},{
				"_ES3Ref" : "2155466466197841777",
				"goID" : "8606208887014283366",
				"slot" : 1,
				"faction" : "",
				"tavernSeats" : [
					{
						"_ES3Ref" : "2757473104712015625"
					},{
						"_ES3Ref" : "5896122006412478740"
					},{
						"_ES3Ref" : "8155609654366732165"
					},{
						"_ES3Ref" : "3081668857995932087"
					}
				]
			},{
				"_ES3Ref" : "5205220558443895360",
				"goID" : "516784855196241593",
				"slot" : 2,
				"faction" : "",
				"tavernSeats" : [
					{
						"_ES3Ref" : "5347194596021911488"
					},{
						"_ES3Ref" : "5970068844136196543"
					},{
						"_ES3Ref" : "5838737796283121499"
					},{
						"_ES3Ref" : "2137992083672095157"
					}
				]
			},{
				"_ES3Ref" : "5703271183222130916",
				"goID" : "8973886442000047297",
				"slot" : 3,
				"faction" : "",
				"tavernSeats" : [
					{
						"_ES3Ref" : "4921097766851121174"
					},{
						"_ES3Ref" : "7460144537797856366"
					},{
						"_ES3Ref" : "7107839831936185012"
					},{
						"_ES3Ref" : "1475154388851829189"
					}
				]
			}
		]
	},
Thanks in advance for any help you can provide!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving List of Custom Class within a Custom Class

Post by Joel »

Hi there,

Fields of UnityEngine.Object types are stored by reference, not value. If you want your List of TavernSeats to be stored you should save them separately so that they can be restored. Then before loading your TavernTables, load your TavernSeats.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply