Saving gameObjects in a scene from another scene question

Discussion and help for Easy Save 3
Post Reply
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Saving gameObjects in a scene from another scene question

Post by Marcus79 »

Hi guys!

I have a question about saving a gameObject in a scene that was brought from another scene. Can this be done with Easy Save 3? Or done at all? Right now I have a simple inventory system that persists throughout scenes, and it works like a charm. I can pick up object and drop them. I can even drop an object if the object was originally from another scene. However, there's a slight issue with that. Let's say I have an axe that I found in the forest. I can drop the axe again with no problem. But let's say I pick up the axe again and go inside a shack in the forest that's another scene. Again, I can select the axe and drop it in the indoor scene. No issue, but when I leave the shack and go back to the forest (no axe in inventory) and then go back again to the shack a second time the object is gone, like it never existed in the first place.

It makes sense because the axe originally spawned in the forest scene. It got transferred to the player's Inventory as a persistent gameObject and therefore can be carried to any scene. But any other scene where the axe doesn't 'live' in doesn't know or have any reference to that object.

So my question is, is it possible to somehow save the gameObject from one scene to another with Es3?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving gameObjects in a scene from another scene question

Post by Joel »

Hi there,

Generally in this case you would use the Saving and Loading Prefab Instances workflow described here:

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

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Re: Saving gameObjects in a scene from another scene question

Post by Marcus79 »

Oh thank you so much!! I just needed a push in the right direction. I wasn't even sure it could be done.

Thanks!
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Re: Saving gameObjects in a scene from another scene question

Post by Marcus79 »

Ok, I took a closer look on how to do this, but I don't think I quite understand the underlying mechanism of the save itself. I understand how to save and load pretty much anything but prefab instances in this scenario.


1. Right-click the prefab and select Enable Easy Save for Prefab.
2. Use ES3.Save to save the instance, or an array of instances if you have multiple instances.
3. To load, use ES3.Load or ES3.LoadInto.
4. Easy Save will create an instance of the prefab if it doesn’t exist in the scene.

I got passed step 1! :D

But where and when do I save the instance? I have something along the lines of

Code: Select all

public string guid = Guid.NewGuid().ToString();

        private string gameObjectPosition;
        private string gameObjectRotation;
        
private void Start()
        {            
            gameObjectPosition = guid + " Position";
            gameObjectRotation = guid + " Rotation";


            
        }

private void OnDisable()
        {
            if (dropped)
            {
                ES3.Save(guid, prefab, GameManager.Instance.SaveFile);
                ES3.Save(gameObjectPosition, GameManager.Instance.SaveFile);
                ES3.Save(gameObjectRotation, GameManager.Instance.SaveFile);
                                
            }           

        }
I assumed to also save the position and rotation of the gameObject so it can be used after instantiating it. But I have a feeling I'm approaching this all wrong since start gets called each time a scene loads. This is where I don't understand saving the 'instance'.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving gameObjects in a scene from another scene question

Post by Joel »

Hi there,
Marcus79 wrote: Thu Oct 26, 2023 9:21 am But where and when do I save the instance?
When and where you save would depend on your project. For example some people save in OnApplicationQuit or before the player changes scene or presses a Save button, but it very much depends on your project.

Just something to note, your code is wrong. The first parameter to ES3.Save is the key, but you seem to be providing the data you're saving. Please see the Getting Started guide for more info:

https://docs.moodkie.com/easy-save-3/getting-started/
I assumed to also save the position and rotation of the gameObject so it can be used after instantiating it. But I have a feeling I'm approaching this all wrong since start gets called each time a scene loads. This is where I don't understand saving the 'instance'.
By default the Transform of the prefab instance will be saved, so you don't need to save it's position separately.

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