Save a list of objects in the scene

Discussion and help for Easy Save 3
Post Reply
Nerevar98
Posts: 2
Joined: Thu Jan 09, 2020 2:54 pm

Save a list of objects in the scene

Post by Nerevar98 »

Hi there! I'm brand new to the forums and to ES3 so there's likely a lot that I'll be asking here haha but today I'm working on trying to save multiple objects on the scene without having to create a string key for each.

So basically I have a SaveManager in my scene that uses FindObjectsOfType to find an array of objects of a type in the scene. Once the array is built, I want to to save the transform of each object on this array onto the file, and then be able to load it back up.

Up until now though, the only sort of thing I've done with ES3 is stuff like this:

Code: Select all

ES3.Save<Transform>("playerTransform", player.transform);
So if I'm trying to save multiple object transforms in a method, then I would guess there's some way to do this without having to manually type in a string for each one right?

I thought maybe that I could type in an initial string and then have an n++ appended onto the string as the for loop saves each object, but I guess I just wanted to check if there was already any kind of functionality built into do this.

Also, if I eventually want to load each transform onto a specific object that it came from how could I do that? Create an ID for each object and save that along side the transform, then check if they match?

Anyway, thanks so much!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save a list of objects in the scene

Post by Joel »

Hi there,

You can find information on saving collections such as arrays/Lists in the Saving and Loading Collections and Arrays section of the Getting Started guide (it works much in the same way as loading data).

In your case, you might want to use ES3.LoadInto rather than ES3.Load. Instead of creating a new instance of your Transform if it doesn't exist, it loads it into an existing Transform. In the case of using LoadInto with arrays, it will load the Transform at the position in the array you are loading into the Transform in the corresponding position you've provided as a parameter.

For example:
// Let's say we save 'tArray', which is an array of 3 Transforms
ES3.Save<Transform[]>("transforms", tArray);
// 'newArray' should also contain 3 Transforms.
// The data from tArray[0] will be loaded into newArray[0]
// The data from tArray[1] will be loaded into newArray[1]
// The data from tArray[2] will be loaded into newArray[2]
ES3.LoadInto<Transform[]>("transforms", newArray);
Note that it's not possible to load a specific item from the saved array, you must load the entire array.

Another quick thing to note: FindObjectsOfType is not guaranteed to load items in the same order each time. If the objects exist in the scene prior to runtime, Instead it is usually recommended to drag the items into an array field in the Editor and use this to ensure the order is always the same.

If the Transforms you're saving are part of prefabs instantiated at runtime, you might want to look at this example:
https://moodkie.com/forum/viewtopic.php?f=16&t=1438

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Nerevar98
Posts: 2
Joined: Thu Jan 09, 2020 2:54 pm

Re: Save a list of objects in the scene

Post by Nerevar98 »

Thanks so much for all the help Joel!

Looks like Dictionaries would be a good basic answer for what I want to do. I haven't looked at the second link you provided to that topic so maybe this is answered there, but I do have a follow up question:

If I want to save enemies in my scene this way, where each enemy class has a set of properties that need to be saved (like name, id, health, position, etc), is there any way to save a class like that using ES3?

I was previously doing this using my own save manager created from scratch which was a serious pain to keep up with compared to ES3, where I created a save object and it had a bunch of properties.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save a list of objects in the scene

Post by Joel »

Hi there,

You can save your Enemy class (and Collections of your Enemy class) in the exact same way you would do with your Transforms, or any other class for that matter. See the Supported Types guide for more info on what can be saved.

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