Page 1 of 1

Referenced objects not persisting?

Posted: Thu Jul 25, 2019 12:01 am
by jeffsim
Here's what's going on:
1. I have an object (based on monobehavior) that I can save and load using ES3 just fine.
2. I have a container object (also based on monobehavior) which references that object. When I Save and then Load the container, the reference to the contained object is null on load.
3. I have gone into the ES3 types window and created types for both objects, and ensured all fields are checked.

Here is the simplest (contrived) test case I could create:

Code: Select all

public class TestContainedObject : MonoBehaviour
{
    public string TestString;
}

public class TestContainer : MonoBehaviour
{
    public TestContainedObject TestObject;
}

public class EntityMgr : MonoBehaviour
{
    public void TestLoad()
    {
        TestContainedObject containedObj = ES3.Load<TestContainedObject>("test-ContainedObject");
        TestContainer container = ES3.Load<TestContainer>("test-Container");

        // At this point: containedObj is valid, but container.TestObject, which points at containedObj, is actually null.
    }

    public void TestSave()
    {
        // set up object
        GameObject objectGO = new GameObject();
        TestContainedObject containedObject = objectGO.AddComponent<TestContainedObject>();
        containedObject.TestString = "hello";

        // Set up container
        GameObject containerGO = new GameObject();
        TestContainer container = containerGO.AddComponent<TestContainer>();
        container.TestObject = containedObject;

        ES3.Save<TestContainedObject>("test-ContainedObject", containedObject);
        ES3.Save<TestContainer>("test-Container", container);
    }
Apologies if I'm missing something obvious about how ES3 works, but thanks for your help!

Jeff

Re: Referenced objects not persisting?

Posted: Thu Jul 25, 2019 5:05 pm
by Joel
Hi Jeff,

As you're creating the Component at runtime, the reference manager has no way of knowing that it has been created, so an instance ID cannot be resolved for it. Note that fields of UnityEngine.Object types are stored by reference.

You can register the reference yourself using the following code:
GameObject.Find("Easy Save 3 Manager").GetComponent<ES3ReferenceMgr>().Add(yourComponent, (long)12345);
If you're creating this Component every time you run the app, you will need to register it with the same reference ID each time (i.e. use 12345 as the parameter each time, or a reference ID of your choosing).

All the best,
Joel

Re: Referenced objects not persisting?

Posted: Fri Jul 26, 2019 5:50 pm
by jeffsim
Ah, that makes sense.

Thanks!
Jeff