Referenced objects not persisting?

Discussion and help for Easy Save 3
Post Reply
jeffsim
Posts: 9
Joined: Tue Jul 23, 2019 2:02 pm

Referenced objects not persisting?

Post 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
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Referenced objects not persisting?

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
jeffsim
Posts: 9
Joined: Tue Jul 23, 2019 2:02 pm

Re: Referenced objects not persisting?

Post by jeffsim »

Ah, that makes sense.

Thanks!
Jeff
Post Reply