Appending Save/Load Data - How is goID generated?

Discussion and help for Easy Save 3
Post Reply
macros976
Posts: 2
Joined: Mon Dec 05, 2022 3:47 pm

Appending Save/Load Data - How is goID generated?

Post by macros976 »

Hello,

ES3 works great for me. Thank you. I've added a feature that allows selected data in my app to be saved/loaded. This works well, except for 1 edge case: Sometimes the original object (goID) is still there (but with different properties). In that case, I want to create a new instance, rather than loadInto the existing one.

In order to do that, I need to check unique IDs. Before I implement that myself, I wanted to see if I could do it using the ES3 property "goID".

How is goID generated for an ES3 file? Can I look for goID in an existing gameobject at runtime?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Appending Save/Load Data - How is goID generated?

Post by Joel »

Hi there,

The goID is just the reference ID of a GameObject, so you can lookup a reference ID using ES3ReferenceMgr.Current.Get(id), presuming that GameObject still exists.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
macros976
Posts: 2
Joined: Mon Dec 05, 2022 3:47 pm

Re: Appending Save/Load Data - How is goID generated?

Post by macros976 »

I was able to achieve what I wanted (and what you suggested) by adding some code inside the ES3ComponentType.ReadObject<T>(ES3Reader reader) method.

Code: Select all

                else if (propertyName == gameObjectPropertyName)
                {
                    //ES3 Source Code
                    //long goID = reader.Read_ref();

                    //if (instance != null)
                    //    break;

                    //var go = (GameObject)refMgr.Get(goID, type);

                    //My Attempt Below
                    long goID = reader.Read_ref();

                    objExists = ES3ReferenceMgr.Current.Get(id);

                    if (instance != null && objExists == null)
                        break;

                    var go = (GameObject)refMgr.Get(goID, type);

                    if (objExists != null)
                    {
                        go = null;
                        goID = Simulator.Utils.GetNewEasySaveReferenceID();
                        id = Simulator.Utils.GetNewEasySaveReferenceID();
                    }

                    if (go == null)
                    {
                        go = new GameObject("Easy Save 3 Loaded GameObject");
#if UNITY_EDITOR
                        go.AddComponent<ES3InspectorInfo>().SetMessage("This GameObject was created because Easy Save could not find a GameObject in the scene with the same instance ID as the GameObject the Component we are loading is attached to.\nTo prevent this from being created, use the LoadInto methods to tell Easy Save what Component the data should be loaded in to.");
#endif
                        refMgr.Add(go, goID);
                    }
                    instance = GetOrAddComponent(go, type);
                    refMgr.Add(instance, id);
                    break;
                }
                
While I don't like modifying source code, all the references I need are right there and this seems like the least disruptive option to an already working system. Would you agree? Or is there a better/simpler way?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Appending Save/Load Data - How is goID generated?

Post by Joel »

Hi there,

As far as I can tell you can achieve the same thing by using ES3.LoadInto on a new instance. I.e.

var go = Instantiate(prefab);
ES3.LoadInto(“key”, go);

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