How can i save a list of gameobjects?

Discussion and help for Easy Save 3
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can i save a list of gameobjects?

Post by Joel »

Hi there,
but it does not load the variables mannualy assigned as a prefab
I'm not sure I understand what this means, please could you clarify?

Also as mentioned in the guide, you shouldn't simply select all of the Components on an ES3GameObject unless you know they're serializable and need to be saved. For example the ES3GameObject itself and ES3Prefabs shouldn't be selected in most situations.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Re: How can i save a list of gameobjects?

Post by Recktardeded »

Hello, thanks for answering. there is a script i ve made that doesn t reload, but i think i can rework the script to work propely. Also how do i know if a script is serializable? i ve read the docs and they say that it should only include variables such as enums,lists,floats,etc. Right? Also, does a variable need to be set as public or [SerializeField] private to be able to be saved?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can i save a list of gameobjects?

Post by Joel »

Could you be more specific when you say it doesn't reload: is the script missing from the loaded object, or are it's variables left as their default values?

Regarding what fields of a script will be saved, please see the Supported Types and Choosing what is saved guides:

https://docs.moodkie.com/easy-save-3/es ... ted-types/
https://docs.moodkie.com/easy-save-3/es ... -is-saved/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Re: How can i save a list of gameobjects?

Post by Recktardeded »

There is a null reference error that is given by a function, but i m preety sure that issue is on my end. I would like to split the current big script in 2 parts: 1. the values of progress and 2. the functionality. Before i continue, the script is about a plant that grows, then makes fruit, and it has 3 phases:
1.growing(a float that shows the plant growing up)2.plant producting fruit(a float showing the lifetime of the plant and a int showing the current amount of grown fruit)3. plant dying(plant is ded, so it sets all of the other values to 0). So there are multiple variables, but only those variables change, so i d like to make a smaller script that on awake reads the current values and then adds AddComponent<BigScript>() with the proper values(and initializes the script). I'm thinking this approach would be a bit more optimised aswell :D . Also, ES3 Gameobject(for the plant gameobject) would just save just transform, box collider and plant_Data. Does this sound like a propper way to aproach this? And thank you very much for your time!
this is what the values to get saved would look like:
private string FruitType_Current;
private float Growing_TimeLeft;
private float plantLifeTime;
private int Fruits_CurrentlyGrowAmount;
private Growing_Phases currentPhase;
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can i save a list of gameobjects?

Post by Joel »

Hi there,

I'm not 100% what the purpose of what you're describing would be, but as that's part of your logic I wouldn't be able to assist with that.
Also, ES3 Gameobject(for the plant gameobject) would just save just transform, box collider and plant_Data. Does this sound like a propper way to aproach this?
This could be the right approach, but usually the Collider on objects doesn't change at runtime so you should only mark this to be saved if you're programatically changing the collider.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Re: How can i save a list of gameobjects?

Post by Recktardeded »

I don t think i undestand the saving of gameobjects and the attatched gameobjects fully. I would like to save a list of gameobjects where each gameobject has a mesh(or more), a box collider, a few children as meshes that get enabled dinamically and the script "Plant_Planted" that goes trough a few enums to change the current sage of the plant_Planted script. My question is, how can i save each gameobject with its properties and its values(plant lifetime, grown fruits, etc)?
As it is right now i usethese functions to save:
public void Garden_SavePlantsList()
{
ES3.Save<List<GameObject>>("PlantList", garden_PlantedPlantsList);
Debug.Log("Saved plant");
}
public void Garden_LoadPlantsList()
{
garden_PlantedPlantsList = ES3.Load<List<GameObject>>("PlantList", new List<GameObject>());
Debug.Log("loaded plant list");
}
but it only loads the plant as the enum has it s final value
Notso
Posts: 51
Joined: Sat May 08, 2021 6:53 pm

Re: How can i save a list of gameobjects?

Post by Notso »

(Please use the code tags when posting code... there is button in the chat box for it, makes it easier to read)
Sorry to jump in here and hope to not make matters worse but it looks like you might be over complicating things for easysave.
I had the same issue and the solution was much simpler than expected.
Try making a list of your plants (you have)
Make sure they are prefabs, then in the assets folder (NOT the scene) right click and make es3 prefab out of them, do not add the script manually.
Then in the scene add the plants to the list you want to use (I would use just 1 to troubleshoot, not all of them).
Forget about the es3gameobject script for now, might not even need it.

Then instead of

Code: Select all

ES3.Save<List<GameObject>>("PlantList", garden_PlantedPlantsList);
use

Code: Select all

ES3.Save("PlantList", garden_PlantedPlantsList);
and for load

Code: Select all

garden_PlantedPlantsList = ES3.Load("PlantList", garden_PlantedPlantsList);
and if that does not work, maybe try

Code: Select all

ES3.LoadInto("PlantList", garden_PlantedPlantsList);
You are making a new list from the save of game objects and not even loading in the save values. It appears that way anyway.
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Re: How can i save a list of gameobjects?

Post by Recktardeded »

Hello, thank you for your Reply! This does not seem to work, but i m thinking of working with a different approach. I am thinking about making a a float that controls the entire plant, and i would save just a list of floats, and each plant would have the name of the gameobject it si referncing so it would looke like

Code: Select all

ES3.Save<float>(gameobject.name,"plants.es3")
and for loading i would do something like:

Code: Select all

foreach(string plant in es3.GetKeys"plants.es3")
{
    GivePlantTypeByString(plant);
    loadedPlant = InstantiatePlantByType(plant);
    loadedPlant.Planted_Plant.progressFloat = ES3.Load(plant,"plants.es3")
    mainList.Add(loadedPlant);
}
Do you think this is a good way of going about?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can i save a list of gameobjects?

Post by Joel »

Thanks Notso :)

@Recktardeded: Just to double-check, have you followed the Saving and Loading GameObjects guide? I had assumed you had, but I suspect you might not have (this is the workflow Notso is describing):
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/
Recktardeded wrote: Wed Mar 06, 2024 5:48 pm Hello, thank you for your Reply! This does not seem to work, but i m thinking of working with a different approach. I am thinking about making a a float that controls the entire plant, and i would save just a list of floats, and each plant would have the name of the gameobject it si referncing so it would looke like

Code: Select all

ES3.Save<float>(gameobject.name,"plants.es3")
and for loading i would do something like:

Code: Select all

foreach(string plant in es3.GetKeys"plants.es3")
{
    GivePlantTypeByString(plant);
    loadedPlant = InstantiatePlantByType(plant);
    loadedPlant.Planted_Plant.progressFloat = ES3.Load(plant,"plants.es3")
    mainList.Add(loadedPlant);
}
Do you think this is a good way of going about?
It's difficult for me to know whether this is a good way of going about it as it mostly relates to your logic rather than saving. If you can recreate your objects from a float then it would be the most efficient way to do so. Instead of saving separate keys to the file it would be more efficient to maintain a List of your floats and save/load this. However, if the method you've described works and is the simplest way for you, I see no reason not to do it.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Re: How can i save a list of gameobjects?

Post by Recktardeded »

Let me clarify, i ve followed the saving of gameobjects tutorial, and it works, it does load the gameobject(mesh, colliders,the needed script,etc), but it does not load the gameobject with the right values as, so that mean i need to simplify the script, but i will simplify it to try to make it work with the float, so it could be even more optimised. Thank you for your feedbacks! It means a lot.<3
Post Reply