List of class won't save

Discussion and help for Easy Save 3
Post Reply
timps
Posts: 2
Joined: Sat Aug 07, 2021 2:14 pm

List of class won't save

Post by timps »

Hey team, I feel like I am missing something obvious somewhere...

I have a class, and a list of the class. It saves without errors, and gives me all the header data, but none of the save data.

The class

Code: Select all

  [System.Serializable]
    public class ObjectList
    {
        [SerializeField]
        public int builtPiece;
        [SerializeField]
        public Vector3 builtPosition;
        [SerializeField]
        public Quaternion builtRotation;

        public ObjectList( int firstone, Vector3 secondone, Quaternion thirdone)
        {
            builtPiece = firstone;
            builtPosition = secondone;
            builtRotation = thirdone;
        }

        public ObjectList() { }

       
    }

    [ES3Serializable]
    public List<ObjectList> placedObjects = new List<ObjectList>();
And the save function is called like this:

Code: Select all

ES3.Save<List<ObjectList>>("BuiltObjects", placedObjects);
The save file gives me this:

Code: Select all

{
	"BuiltObjects" : {
		"__type" : "System.Collections.Generic.List`1[[BuildSystem+ObjectList, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]],mscorlib",
		"value" : [
			
		]
	}
}
I can save an array of a custom class just fine. But this list won't save anything.
Help please!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: List of class won't save

Post by Joel »

Hi there,

This appears to be working fine at my end. I've replicated this with the following script:

Code: Select all

using System.Collections.Generic;
using UnityEngine;

public class ObjectListTest : MonoBehaviour
{
    public List<ObjectList> placedObjects = new List<ObjectList>();

    void Start()
    {
        placedObjects.Add( new ObjectList(1, Vector3.zero, Quaternion.identity) );
        placedObjects.Add( new ObjectList(2, Vector3.zero, Quaternion.identity) );
        placedObjects.Add( new ObjectList(3, Vector3.zero, Quaternion.identity) );

        ES3.Save("BuiltObjects", placedObjects);

        Debug.Log(ES3.LoadRawString());
    }

    public class ObjectList
    {
        public int builtPiece;
        public Vector3 builtPosition;
        public Quaternion builtRotation;

        public ObjectList(int firstone, Vector3 secondone, Quaternion thirdone)
        {
            builtPiece = firstone;
            builtPosition = secondone;
            builtRotation = thirdone;
        }

        public ObjectList() { }
    }
}
This outputs the following:
{
"BuiltObjects" : {
"__type" : "System.Collections.Generic.List`1[[ObjectListTest+ObjectList, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]],mscorlib",
"value" : [
{
"builtPiece" : 1,
"builtPosition" : {
"x" : 0,
"y" : 0,
"z" : 0
},
"builtRotation" : {
"x" : 0,
"y" : 0,
"z" : 0,
"w" : 1
}
},{
"builtPiece" : 2,
"builtPosition" : {
"x" : 0,
"y" : 0,
"z" : 0
},
"builtRotation" : {
"x" : 0,
"y" : 0,
"z" : 0,
"w" : 1
}
},{
"builtPiece" : 3,
"builtPosition" : {
"x" : 0,
"y" : 0,
"z" : 0
},
"builtRotation" : {
"x" : 0,
"y" : 0,
"z" : 0,
"w" : 1
}
}
]
}
}
Are you sure you've added the items to your List before saving it? The save data denotes an empty list.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
timps
Posts: 2
Joined: Sat Aug 07, 2021 2:14 pm

Re: List of class won't save

Post by timps »

Thanks for the answer Joel. Really appreciate it.

Premium customer service right here.
Copying yours (or mine) into the start function saved the test data just fine.
And the same worked when moving the code elsewhere. But not in the original method.

Which has helped me track down what's going on entirely.
This one was all user error.
The save function was referencing the variable on another copy of the prefab.
Fixing that sorted everything out.

I'll go leave a well deserved review for the asset right now.
Again, thanks for the quick reply. :)
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: List of class won't save

Post by Joel »

Glad that helped you identify the issue, and thanks for the kind review :)

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