LoadInto overwrite reference

Discussion and help for Easy Save 3
Post Reply
Ell223
Posts: 10
Joined: Thu Jan 07, 2021 2:42 pm

LoadInto overwrite reference

Post by Ell223 »

Hello,

In my game, for various reasons, I am saving components of prefabs manually using Save on each component. When I load this data back, I instantiate the prefab relating to the component myself, and then use LoadInto to override the data with the saved data. However as I understand it, this also retains the new reference ID that is generating when I instantiate the prefab, rather than applying the reference of the component that is in the save. When I load further objects which reference these components, they can't find them, and create a new gameobject.

Is there any way for me to use LoadInto, and also overwrite the reference of the component I am loading into, with the reference from the save file?

Thanks.
Ell223
Posts: 10
Joined: Thu Jan 07, 2021 2:42 pm

Re: LoadInto overwrite reference

Post by Ell223 »

Actually think this might be related to the way I am saving the reference. I am using a Dict<string, object>. When I load this back, it doesn't seem to find the reference and creates a new Gameobject with the component on it. I think ES3 is saving the object with its values, has "_type", "goID" and includes all the data I include if I were to save it directly. Whereas I just want it to save a reference to the component. How can I go about this?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: LoadInto overwrite reference

Post by Joel »

Hi there,

If I understand correctly, you need to save your objects separately and load them before loading anything which references them.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Ell223
Posts: 10
Joined: Thu Jan 07, 2021 2:42 pm

Re: LoadInto overwrite reference

Post by Ell223 »

Sorry I don't think I explained very well. Saving and loading a reference directly works fine. And looks like e.g this in the save file:

Code: Select all

"<workplace>k__BackingField" : {
	"_ES3Ref" : "3321872692111343812"
},
With types class saving like:

Code: Select all

writer.WritePrivateFieldByRef("<workplace>k__BackingField", instance);
However if I save a dictionary of string, object named 'data' inside of a struct named 'jobSaveInfo', it seems to save the reference as it were saving the gameobject itself (which is saved earlier in the file), if that makes sense, and looks like so in the save file:

Code: Select all

"jobSaveInfo" : {
	"<id>k__BackingField" : "JobConstruct",
	"<data>k__BackingField" : {"constructionSite":{
		"__type" : "GG.Buildings.ConstructionSite,GG.Buildings",
		"_ES3Ref" : "1153516979115495572",
		"goID" : "3773226602233100395",
		"<labourTimeHave>k__BackingField" : 0
		}
	}
So when this gets loaded the system seems to think it's a new object, and creates a gameobject for the component to go onto, rather than being linked back to the reference.

I am assuming this is some issue relating to the fact it's being saved as an 'object' rather than it's explicit type, and was wondering if you had any suggestions on how to fix this, or if it can be fixed.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: LoadInto overwrite reference

Post by Joel »

Hi there,

Please could you create a new project with a very simple scene which demonstrates this issue and private message it to me with step-by-step instructions so that I can see what is happening.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: LoadInto overwrite reference

Post by Joel »

Thanks for sending over the project, that's helped me understand the issue.

Currently we require the reference ID of the Component and GameObject to match for it to resolve correctly. If one doesn't match, a new instance of both the Component and GameObject is created.

Although to a serializer this may make sense, this isn't ideal in a few circumstances such as your own. I've added an edge case to the serializer which ignores the GameObject reference if the Component reference resolves, which should resolve the issues you were seeing. I'll PM this update over to you in a moment.

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