Saving and Loading multiple game objects

Discussion and help for Easy Save 3
ProjectMarbles
Posts: 6
Joined: Sat Dec 07, 2019 8:22 pm

Saving and Loading multiple game objects

Post by ProjectMarbles »

Hey, guys so hopefully you'll be able to help me out with this one. For the game that' I'm developing there are multiple message game objects that are being instantiated as prefabs in a kind of queue. That's the entire game in a nutshell. Which is why I'm hoping to use easy save to save and load the messages when the player quits. Leaving the player exactly where they left off.

There are components on the gameobjects that load the next message in the array so when using the autosave method for EasySave there are just duplicates of everything. So I was trying to use the manual save and load however that's also not working as well as I would want it to. So I'm a bit lost is there a way to save and load multiple gameobjects while also disabling the components of them?

Thank you so much !
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading multiple game objects

Post by Joel »

Hi there,

Please could you provide a bit more information on how you are attempting to save and load at the moment? Otherwise the question is a bit too broad for me to answer :)

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
ProjectMarbles
Posts: 6
Joined: Sat Dec 07, 2019 8:22 pm

Re: Saving and Loading multiple game objects

Post by ProjectMarbles »

Hi there!

Sorry it's a bit broad, here's a bit more information! Right now I'm attempting to save and load using the method and example in the PrefabManager script included in Easy Save as well as the topic on this forum called Saving and Loading Prefabs at Runtime. I've also used basic ES3.Save<GameObject> and load ES3.Load<GameObject> however no matter what method I use the gameobject that I'm trying to save.
Image
Image
Image


This is how the game looks and functions. Messages are loaded in at runtime with animation and the player makes choices with a mouse click. What I'm hoping to save is all the messages that have been instantiated or if thats not possible save and load the last picked choice which would then resume the animation.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading multiple game objects

Post by Joel »

Hi there,

When using ES3.Save<GameObject> and ES3.Load<GameObject>, have you following the saving and loading prefab instances guide here?
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

This is different to the Saving and Loading Prefab Instances example, which is just a guide to show you how you can manually save your own prefabs without the manager.

If you have followed these instructions, please could you post the script you're using to save and load? It's hard to tell what is happening without seeing code.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
ProjectMarbles
Posts: 6
Joined: Sat Dec 07, 2019 8:22 pm

Re: Saving and Loading multiple game objects

Post by ProjectMarbles »

Hello!

I am following the saving and loading prefab instances guide. I can tell it's different though I dont really understand the point of either. But here's the code for the script I'm using. The script is called: SaveGameManager and it's a script located on a Game Manager object.

Code: Select all

// Prefab we wanted to create 
    public GameObject message;
   


 
    [HideInInspector]
    public bool lillianaAlive = true;
    [HideInInspector]
    public bool averyAlive = true;
    [HideInInspector]
    public bool momFinished = false;

    [HideInInspector]
    public string levelToLoad;

  

    // This is called whenever the application is quit. This way data will be saved even if the game is saved
    private void OnApplicationQuit()
    {
        MessageSave();
        SaveVariables();
    }

    private void Start()
    {
        LoadVariables();
        MessageLoad();
    }




    //This is where the gameobjects are going to be saved.  
    public void MessageSave()
    {
    
        ES3.Save<GameObject>("messageSaved", message);
        Debug.Log("GAME WAS SAVED");
        

    }

    //This is where the gameobjects are going to be loaded 
    public void MessageLoad()
    {
        ES3.Load<GameObject>("messageSaved");
        Debug.Log("GAME WAS LOADED");
    }

  

    
    // All NON Message loading stuff is located below these functions can be called whenever: 

    public void SaveVariables()
    {
        //Saving the Scene name and the status of Lilliana and Avery and mom
        ES3.Save<string>(SceneManager.GetActiveScene().name, "savedScene");
        ES3.Save<bool>("lillianaStatus", lillianaAlive);
        ES3.Save<bool>("averyStatus", averyAlive);
        ES3.Save<bool>("momStatus", momFinished);
    }

    public void LoadVariables()
    {
        //Loading the Status of Avery & Lilliana
        lillianaAlive = ES3.Load<bool>("lillianaStatus");
        averyAlive = ES3.Load<bool>("averyStatus");
        if(SceneManager.GetActiveScene().name == "Prolouge2")
        {
            momFinished = ES3.Load<bool>("momStatus");
        }
        
    }

    public void LoadLevel()
    {
        levelToLoad = ES3.Load<string>("savedScene");
        SceneManager.LoadScene(levelToLoad);
    }

The code relating to bools or levels does work and is only there because those variables have to be saved and loaded as well, it's mainly just the gameobjects i'm confused on. Right now this code does. Nothing. So I'm generally confused on what is supposed to be happening or if I'm doing something wrong. All the messages are prefabs.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading multiple game objects

Post by Joel »

Hi there,

In your code you seem to be saving the 'Message' prefab, rather than the instances of the prefab you are creating?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
ProjectMarbles
Posts: 6
Joined: Sat Dec 07, 2019 8:22 pm

Re: Saving and Loading multiple game objects

Post by ProjectMarbles »

Hi Joel!

Sorry for the late reply and all that but happy new year. My team and I have been trying numerous solutions together but never getting anything concrete down. I know you said to look at the instances of the prefab being created but I'm not sure exactly how to save that or if that would work with saving multiple prefabs.

In fact just, in general, we're pretty lost. If you've got any kind of solution that would help that'd be great. We're pretty lost on how exactly to save multiple objects.


Thanks,
Lawrence
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading multiple game objects

Post by Joel »

Hi Lawrence,

To save multiple GameObjects you would simply save an array or List of them. More info on this can be found in the Saving and Loading Collections section of the Getting Started guide: https://docs.moodkie.com/easy-save-3/getting-started/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
faris_knight
Posts: 2
Joined: Thu Jan 16, 2020 3:20 pm

Re: Saving and Loading multiple game objects

Post by faris_knight »

I just bought this Asset and its so powerful, covering all I need, thanks

I want to try to answer this problem.

first of all, by saving a List of Gameobject does not save the game object itself. it's just saving the address of the game object that appears in the hierarchy.
it's different for primitive types such as int, float, or bool. with this kind of type, the value is saved.

because the field in every element of a List just contains the address of a game object, when you destroy the game object, the element in the field became missing or empty. re-instantiate does not fix this problem because that new instance is a different game object.

what we need to do:
1. follow the guide for enabling saving prefab instance. in the game, we will instance this prefab and have some element or field reference to it. if this particular object destroyed, all of the field reference to this object will go empty.
2. we need to save both the object and the List. save the List as List of Game Object.
3. for saving the object, I suggest place all of them under one parent, and just save the parent with save children enable in easy save runtime setting.
4. it's done for saving.
5. to load, make sure load the saved GameObject first, then the List of GameObject, if we do otherwise the element will go empty or missing because it can not find the GameObject its reference to.
6. And Done

that's All, I Hope i help someone.

Faris Knight
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading multiple game objects

Post by Joel »

Thank you Faris, really appreciate you taking the time to help others on the forum :)

Just to clarify, saving a List<GameObject> should save the GameObject by reference and value. However, if saving a script with a field containing a List<GameObject>, the GameObjects in that List will be saved by reference only.

Also note that only Components on the GameObject which are natively supported or have had support added in the Types panel will be saved (more on this in the GameObject guide)

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