Save and Load Prefabs changes.

Discussion and help for Easy Save 3
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Save and Load Prefabs changes.

Post by Loki180 »

Hello,

I just got the asset and so far I'm feeling good about it.

Easy Save saves a Prefab pretty easily BUT I also need it to save the Prefabs changes, such as if a child of the prefab has been activated or deactivated or deleted or a change in a child's Material.

Is this something that can be done? I can't save the prefab either as it will overwrite all of the Prefabs.

I can get a list of GameObjects and Save them with new save names but how do I load them without knowing the names?

Using this code to save

Code: Select all

private int wallsNum;

    public void SaveGame()
    {
        foreach(wallData i in GameObject.FindObjectsOfType<wallData>())
        {
            ES3.Save<GameObject>("newWall" + wallsNum++, i.gameObject);
            ES3.Save<int>("amountWalls", wallsNum);
            print("newWall" + wallsNum);
        }
        ES3.Save<int>("moneyValue", money.currentMoney);
        ES3.Save<bool>("tutDone", jellyPad.tut);
    }

    public void LoadGame()
    {
        int moneyValue = ES3.Load<int>("moneyValue");
        bool tutDone = ES3.Load<bool>("tutDone");
        int amountWalls = ES3.Load<int>("amountWalls");

        money.currentMoney = moneyValue;
        jellyPad.tut = tutDone;

        while (amountWalls >= 0)
        {
            ES3.Load<GameObject>("newWall" + amountWalls--);
            print(amountWalls);
        }
    }
This is what I've got so far. It is working, besides the Prefab changes that the player makes are not saved.

There is also an issue that some loaded GOs are in the wrong position. Not by a large margin but in the wrong position. It seems to effect the last loaded object more than anything. Also when there is a lot of GOs to save it doesn't seem to be able to load them in and says they couldn't be found.

So to recap there is the above issue and not being able to save the Prefabs changes.

Doesn't seem to able to deal with higher numbers of GOs either. I created a simple set up with around 72 GOs and it says it can't find them.

I'm not thinking its creating an extra Prefab. It also creates new Objects that weren't part of the save file once loaded.

I know it's my Code that is killing it. But I'm just not sure where. Something must be wrong with the loop.
But in regards to Saving changes made to Prefabs I have no idea.
Last edited by Loki180 on Thu Oct 03, 2019 3:16 pm, edited 2 times in total.
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Re: Save and Load Prefabs changes.

Post by Loki180 »

Once the load has been loaded some Prefabs are in the wrong positions. Most of the Prefabs are in the correct location but SOME of them are not.

The Prefab changes issue still needs to be sorted out though.

I'm starting to think that I may need to write out my own Save and Loading method and not use the Asset. Seems to be a great Asset for simple saving and Loading though.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save and Load Prefabs changes.

Post by Joel »

Hi there,

I've had no reports of the wrong positions being loaded, or the amount of GameObjects having an effect. However, GameObject.FindObjectsOfType is not guaranteed to return objects in any particular order, so if the order they are loaded is important you might want to assign IDs to the prefab instances rather than using the index. You might also want to use for loops as these tend to be easier to bug (because you can load them in the exact same order as you saved).

To avoid the loops entirely, you could also just save and load a GameObject[] or List<GameObject> containing them all.

Also perhaps one issue to be aware of, you don't appear to be resetting your wallsNum variable before loading, so if loading multiple times this may also cause issues.

And just to check, have you followed the Saving and Loading Prefab Instances instructions here?
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Re: Save and Load Prefabs changes.

Post by Loki180 »

Thank you, for your reply, Joel.

I've changed my code somewhat.

I now have each GameObject Prefab the player creates into the world is added to a List then I loop over the list to save out the GameObjects.

Code: Select all

public void Awake()
    {
        if(mainMenu.loadOnAwake == true)
        {
            LoadGame();
            mainMenu.loadOnAwake = false;
        }
    }

    public void SaveGame()
    {
        int wallsNum = 0;
        foreach(GameObject i in itemDataBase.buildObjs)
        {
            ES3.Save<GameObject>("newWall" + wallsNum++, i.gameObject);
            ES3.Save<int>("amountWalls", wallsNum);
        }

        ES3.Save<int>("moneyValue", money.currentMoney);
        ES3.Save<bool>("tutDone", jellyPad.tut);
        ES3.Save<string>("currentLevel", SceneManager.GetActiveScene().name);
    }

    public void LoadGame()
    {
        int moneyValue = ES3.Load<int>("moneyValue");
        bool tutDone = ES3.Load<bool>("tutDone");
        int amountWalls = ES3.Load<int>("amountWalls");

        money.currentMoney = moneyValue;
        jellyPad.tut = tutDone;

        while (amountWalls >= 0)
        {
            GameObject newObj = ES3.Load<GameObject>("newWall" + amountWalls--);
            itemDataBase.buildObjs.Add(newObj);
        }
    }
Sometimes the issue with adding new Prefabs are fixed so it's an issue with my setup not Easy Save. I'll have to rework my code to see what is causing the problem. It's something to do with the list thats been created.

BUT It's just saving Prefab changes. I've read the Guide you sent and it doesn't seem to answer my question, unless I'm missing something.
If the player changes a Hidden child to active or alters the Material.
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Re: Save and Load Prefabs changes.

Post by Loki180 »

Is ES3.LoadInto<GameObject> what I'm looking for? I'd imagine I'd have to first load in the prefabs and then load in the data to that object?

Code: Select all

GameObject newObj = ES3.Load<GameObject>("newWall" + amountWalls--);
ES3.LoadInto<GameObject>("newWall" + amountWalls, newObj);
The above doesn't work, but am I on the right track?
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save and Load Prefabs changes.

Post by Joel »

Hi there,

Have you tried saving the list itself, rather than looping over the List? This will rule out it being an issue with how you are iterating over the GameObjects, which I think is the most likely cause based on the behaviour you're getting.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Re: Save and Load Prefabs changes.

Post by Loki180 »

I'll give that ago in the morning. I'll let you know how it turns out.

Thank you for the help so far btw.
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Re: Save and Load Prefabs changes.

Post by Loki180 »

So I've worked through the Guides in attempt to create this.

Code: Select all

public void Awake()
    {
        if(mainMenu.loadOnAwake == true)
        {
            LoadGame();
            mainMenu.loadOnAwake = false;
        }
    }

    public void SaveGame()
    {
        ES2.Save(itemDataBase.buildObjs, "gameObjs");

        ES3.Save<int>("moneyValue", money.currentMoney);
        ES3.Save<bool>("tutDone", jellyPad.tut);
        ES3.Save<string>("currentLevel", SceneManager.GetActiveScene().name);
    }

    public void LoadGame()
    {
        int moneyValue = ES3.Load<int>("moneyValue");
        bool tutDone = ES3.Load<bool>("tutDone");
        itemDataBase.buildObjs = ES2.LoadList<GameObject>("gameObjs");


        money.currentMoney = moneyValue;
        jellyPad.tut = tutDone;

        foreach(GameObject i in itemDataBase.buildObjs)
        {
            GameObject newLoadedGO = Instantiate(i.gameObject, i.transform.position, i.transform.rotation) as GameObject;
        }
    }
The above is much faster to save and load BUT it just creates empty GameObjects. Am I doing this correctly?

Thank you for your help so far.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save and Load Prefabs changes.

Post by Joel »

Hi there,

You seem to be mixing ES2 and ES3 methods. I recommend that you only use Easy Save 3 methods as ES2 is only included for backwards compatibility.

You can find info on how to do this here:
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Loki180
Posts: 10
Joined: Wed Oct 02, 2019 6:35 pm

Re: Save and Load Prefabs changes.

Post by Loki180 »

This is my updated Code.

It still doesn't save out the changes made to Prefabs however.

Code: Select all

public void Awake()
    {
        if(mainMenu.loadOnAwake == true)
        {
            LoadGame();
            mainMenu.loadOnAwake = false;
        }
    }

    public void SaveGame()
    {
        ES3.Save <List<GameObject>> ("gameObjs", itemDataBase.buildObjs);


        ES3.Save<int>("moneyValue", money.currentMoney);
        ES3.Save<bool>("tutDone", jellyPad.tut);
        ES3.Save<string>("currentLevel", SceneManager.GetActiveScene().name);
    }

    public void LoadGame()
    {
        int moneyValue = ES3.Load<int>("moneyValue");
        bool tutDone = ES3.Load<bool>("tutDone");

        itemDataBase.buildObjs = ES3.Load<List<GameObject>>("gameObjs");


        money.currentMoney = moneyValue;
        jellyPad.tut = tutDone;

        foreach(GameObject i in itemDataBase.buildObjs)
        {
            GameObject newLoadedGO = Instantiate(i.gameObject, i.transform.position, i.transform.rotation) as GameObject;
        }
    }
I'm sure this is a better build BUT I just need to save changes the player makes to the Prefab. To clarify I don't want to overwrite a Prefab, I need Easy Save to save out GameObjects as they are when they're saved and not just create new instances of the Prefab. We're getting close to getting this done. Testing it again, everything seems to be working great, besides saving out Prefabs changes.

Is Easy Save able to do what I need it too do?

Thank you for helping me get this far!
Post Reply