Field referencing other GameObject is null when loaded

Discussion and help for Easy Save 3
Post Reply
FredTuna
Posts: 20
Joined: Sat Aug 10, 2019 3:16 pm

Field referencing other GameObject is null when loaded

Post by FredTuna »

I've made some progress using ES3 to save my game. There are several references between game objects in my scene which complicates things.

One thing I noticed is that if a GameObject A with Component 1 has a reference to a GameObject B that is lower in the scene (which means it will be saved after), then when I load the Save Data and the GameObject A is loaded and Component 1's fields are being read and populated, it won't find the reference for GameObject B since it hasn't loaded yet.
Am I seeing expected behaviour here? Is there something I can do to fix that problem? Perhaps have ES3 instantiate an empty Game Object when Component 1 tries to find the reference to Game Object B and then when the ES3 reader gets to the part in the Save File where Game Object B is loaded, it would populated components and fields on the empty GameObject which was instantiated earlier.

Any help is appreciated. Thanks!

Fred
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Field referencing other GameObject is null when loaded

Post by Joel »

Hi Fred,

I replied to your email about this on Friday, but it sounds like you might not have received it so I'll post the response here :)

This is expected behaviour. It wouldn't be possible to automatically create the GameObject because this would prevent cases where the lac of reference indicates that it should be set to null (which is very common).

The way around this would be to save your GameObject field by reference and value, rather than just by reference.

You can do this by creating an ES3Type file for the Component containing the GameObject field (see this guide if you haven't done this already: https://docs.moodkie.com/easy-save-3/es ... -es3types/), and the change the line in the WriteComponent method to use WriteProperty rather than WritePropertyByRef. I.e. Change this:
writer.WritePropertyByRef("go", instance.go);
To this:
writer.WriteProperty("go", instance.go, ES3.ReferenceMode.ByRefAndValue);
Hope this helps!

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
FredTuna
Posts: 20
Joined: Sat Aug 10, 2019 3:16 pm

Re: Field referencing other GameObject is null when loaded

Post by FredTuna »

I guess I never received your email but thanks for your answer here!!

That seem to have done the trick. Since it was a private field, I had to create WritePrivateField() variant that takes in a ES3.ReferenceMode parameter in ES3Writer.cs.

Thanks again :)
Post Reply