ES3: Question saving a list of object active states

Discussion and help for Easy Save 3
Ezo
Posts: 3
Joined: Tue Feb 13, 2018 2:05 am

ES3: Question saving a list of object active states

Post by Ezo »

Howdy,

Been using EasySave for my project I've been working on .

One hurdle I've come across is being able to save a list of gameobject Active States once the item is innactive on the hierarchy. I used the below snippet of code in ES2 for testing.

Code: Select all

    public void OnDestroy()
    {
        // Save the enabled state when the scene/object is destroyed.
      ES2.Save(gameObject,gameObject.activeSelf, "myFile.txt?tag=active" + gameObject.name);
    }
 
    public void Start()
    {
        // Check if there's an active state to load. If there is, load our active state.
        if(ES2.Exists("myFile.txt?tag=active" + gameObject.name))
            gameObject.SetActive( ES2.Load<bool>( "myFile.txt?tag=active" + gameObject.name ) );
      
    }
}

However, I do not want the item to save it's active state unless the player saves the game. If the player dies and restarts from the last save I don't want the items to be disabled.

I figured that a possibility is to change the code to add the item to a list with it's activeState when OnDestroy() is called. Then that list of items with activestates would be saved when the player actually saves the game. I'd imagine i would just have a loop on the save function to iterate through the collection I am trying to make to save. But I'm struggling getting something like that to work.

I have scriptableObjects that hold the object data during the playthrough. Is it possible to just save those scriptable object files on save?


Any assistance is appreciated! Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES3: Question saving a list of object active states

Post by Joel »

Hi there,

This is possible with ES2 and ES3. I will describe how to do this with ES2, but generally it's the same for both.

Basically instead of getting each object to save it's own active state, you should have a MonoBehaviour with an array containing all of these GameObjects, which contains a Save() method which you can call whenever you want to save.

When you save, this could create an bool array which is of the same length as the GameObject array, which stores whether each GameObject is disabled. You can then set each index based on whether the GameObject is enabled, and save this array using ES2.Save.

Then you could also contain a Load() method, which loads the array you saved using ES2.LoadArray and enables or disables each GameObject in the original array accordingly.

If you wanted to save the ScriptableObjects, you could do add support for the scriptable object using the instructions here: http://docs.moodkie.com/easy-save-2/gui ... her-types/ . It would however be your responsibility after loading the ScriptableObject to use that to enable or disable the GameObject.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Ezo
Posts: 3
Joined: Tue Feb 13, 2018 2:05 am

Re: ES3: Question saving a list of object active states

Post by Ezo »

Thanks for the reply!
Joel wrote: When you save, this could create an bool array which is of the same length as the GameObject array, which stores whether each GameObject is disabled. You can then set each index based on whether the GameObject is enabled, and save this array using ES2.Save.

Then you could also contain a Load() method, which loads the array you saved using ES2.LoadArray and enables or disables each GameObject in the original array accordingly.

The part I am having a hard time wrapping my head around is loading the data from the bool with each item on the game object array with the array of bools. Do you have an example I could work with?


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

Re: ES3: Question saving a list of object active states

Post by Joel »

I realised I've actually made a script which does exactly what you want a few months ago, which I've attached below.

It saves in OnApplicationQuit(), and loads on Start(), but you can simply change the method names to Save() and Load() and call them whenever you want.

All the best,
Joel
Attachments
SaveActiveStates.cs
(1.04 KiB) Downloaded 500 times
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Ezo
Posts: 3
Joined: Tue Feb 13, 2018 2:05 am

Re: ES3: Question saving a list of object active states

Post by Ezo »

Thank you! With a few changes it works exactly how I need!
Alatriste
Posts: 6
Joined: Mon May 08, 2017 7:41 am

Re: ES3: Question saving a list of object active states

Post by Alatriste »

Hi,

I found this post while looking for information about how to save active objects in the scene. I'm using playmaker so my coding skills are limited. :P
I'm not sure if I understood correctly how to use the script you provided. I created a gameObject in my scene and I made a quick test using size 1 and adding one of the objects I'm activating during gameplay. This object is activated when I save the game, but when I load it to that point, the object is disabled.

Any idea how I can get the objects working on loading?

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

Re: ES3: Question saving a list of object active states

Post by Joel »

Hi there,

There's a newer version of this script that you might want to try. Instead of adding GameObjects to an array, you attach this script to all of the GameObjects that you want to save the active state of.

All the best,
Joel
Attachments
SaveActive.cs
(408 Bytes) Downloaded 391 times
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Alatriste
Posts: 6
Joined: Mon May 08, 2017 7:41 am

Re: ES3: Question saving a list of object active states

Post by Alatriste »

Thanks! I'll give a try as soon as soon as possible. :)
Alatriste
Posts: 6
Joined: Mon May 08, 2017 7:41 am

Re: ES3: Question saving a list of object active states

Post by Alatriste »

I added the script to one of the objects but when I load it doesn't do anything.
I created a test scene, with a cube and a string line disabled. Both are activated through a button so I modify the original scene. But when I load, only the string has changed. The cube doesn't show up.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES3: Question saving a list of object active states

Post by Joel »

Hi there,

Please could you PM me a very basic scene and instructions which replicate this? I don't appear to be encountering problems with it at my end.

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