Saving a reference to an object created at runtime?

Discussion and help for Easy Save 3
Post Reply
lockhighjumpingvenus
Posts: 7
Joined: Wed Oct 11, 2023 9:52 pm

Saving a reference to an object created at runtime?

Post by lockhighjumpingvenus »

Hi, I have a button that is created at runtime (cloned from another button), during gameplay there is a script that needs to keep reference to that button. I am saving the script with the reference to the button (saving the transform of the button), but when it loads the reference to the button is lost. Is it possible to keep this reference somehow? I am using autosave? I think the reference is not being assigned with the button because the button is created at runtime.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a reference to an object created at runtime?

Post by Joel »

Hi there,

To do this with Auto Save you would need to make the button a prefab and follow the prefab instructions in the Auto Save guide.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 7
Joined: Wed Oct 11, 2023 9:52 pm

Re: Saving a reference to an object created at runtime?

Post by lockhighjumpingvenus »

Hi, thanks for the response. Could you maybe point me in the direction of any documentation or examples of how I would do this in code, rather than easy save, as I was wanting to stay away from making the object a prefab, as I would have to change a lot of my code. Thanks
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a reference to an object created at runtime?

Post by Joel »

Hi there,

You would follow the Saving and Loading GameObjects guide:

https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

The Saving and Loading References guide is also relevant to you, particularly the last section:

https://docs.moodkie.com/easy-save-3/es ... eferences/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
lockhighjumpingvenus
Posts: 7
Joined: Wed Oct 11, 2023 9:52 pm

Re: Saving a reference to an object created at runtime?

Post by lockhighjumpingvenus »

Thanks for the links. I have messed around with it for a bit, but I am still having some issues. When loading i get the issue of a easy save 3 loaded gameobject being created and assigned to the script that was referencing the button. I assume this is because of the reference ID of the button is changed when it is instantiated at runtime when the game restarts (during every start of the game). What is meant to be done in that case? Sorry for all the questions, appreciate the help greatly.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a reference to an object created at runtime?

Post by Joel »

Hi there,

An ES3 Loaded GameObject if created if the GameObject of the Component you're loading no longer exists with that reference ID.

If you want to load a Component into an existing instance of a Component you will need to use ES3.LoadInto to load the data into that specific instance as it would be impossible for Easy Save to automatically determine which instance it belongs to.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply