Saving a list of gameobjects with only floats gives errors

Discussion and help for Easy Save 3
Post Reply
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Saving a list of gameobjects with only floats gives errors

Post by Recktardeded »

Hello, since last week i kept trying to save gameobjects only with a float and their name. I would read the entire list of gameobjects and i would use the gameobject s name to save it as a key.(the main diffrenece between them is the index which is given based on the list, so you would get apple_1,apple_2,cherry_3 and so on) and it would save a float(also it saves the number of grown fruits as a int) which represents the progress of the plant growing called "progressFloat" which is a float that represents 3 values: 1. if the value is higher the plant life time and plant growth time, then if would continue the growth process,2. if the value is lower than growth time and higher than 0, then it would continue the plant from it s fully growns state and 3. if the progressFloat is 0 then the plant is not available anymore and it would proceed acordingly.And for the loading logic, it would go through each key inside the "plants.es3" folder and it would instantiate a new plant with the specific type and it would call a function that would load the needed values. Now that i went through the saving loading procces, i get these errors:

Code: Select all

InvalidOperationException: Trying to load data of type System.Single, but data contained in file is type of System.Int32.
ES3Reader.ReadTypeFromHeader[T] () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:311)
ES3Reader.Read[T] (System.String key) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:199)
ES3.Load[T] (System.String key, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:459)
ES3.Load[T] (System.String key, System.String filePath) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:433)
Manager_Data.Garden_LoadPlants () (at Assets/Scripts/Managers/Manager_Data.cs:129)
Manager_Data.Awake () (at Assets/Scripts/Managers/Manager_Data.cs:66)
and these are the functions for loading saving (loading get s called on awake)

Code: Select all

public void Garden_SavePlantList()
{
    foreach(GameObject plant in garden_PlantedPlantsList)
    {
        if (plant.gameObject.GetComponent<Plant_Planted>() != null)
        {//save with list
            ES3.Save<float>(plant.name, plant.gameObject.GetComponent<Plant_Planted>().Saving_ReturnProgressFloat(), "plants.es3");
            ES3.Save<int>(plant.name, plant.gameObject.GetComponent<Plant_Planted>().Saving_ReturnFruitNr(), "plants.es3");
        } 
    }
    Debug.Log("Saved plantList");
}
public void Garden_LoadPlants()
{   
    if (ES3.GetKeys("plants.es3") != null)
    {
        foreach (string plantName in ES3.GetKeys("plants.es3"))
        {
            float ProgressFloatPlant = ES3.Load<float>(plantName, "plants.es3");
            int ProgressFruitNr = ES3.Load<int>(plantName, "plants.es3");
            GameObject loadedPlant = Instantiate(GetFruitType(Garden_GetFruitTypeFromString(plantName)).plant_planted_Prefab, GameManager.Instance.manager_Garden.PlantedPlantsReference.transform);
            if (loadedPlant.GetComponent<Plant_Planted>() != null)
            {
                var plantScript = loadedPlant.GetComponent<Plant_Planted>();
                plantScript.InitializePlant_Loaded(GetFruitType(Garden_GetFruitTypeFromString(plantName)), ProgressFloatPlant, ProgressFruitNr);
            }
            loadedPlant.name = plantName;
            GameManager.Instance.manager_Garden.Garden_SetReferenceParent(loadedPlant);
            garden_PlantedPlantsList.Add(loadedPlant);
            Debug.Log("Loaded" + loadedPlant + plantName);
        }
    }
}
As a side note, the ProgressFloat gets updated properly, thus its value represents the value needed.
Also, thank you for your time!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a list of gameobjects with only floats gives errors

Post by Joel »

Hi there,

Your two ES3.Save calls are using the same key. This means that you're just overwriting the float you're saving with the int you're saving. So when you come to load the float, the data will instead be the int that you've saved, which is why you're getting that error.

Usually you would append something to the key to identify what variable it is. I.e.

Code: Select all

ES3.Save<float>(plant.name + "ReturnProgressFloat", plant.gameObject.GetComponent<Plant_Planted>().Saving_ReturnProgressFloat(), "plants.es3");
ES3.Save<int>(plant.name + "ReturnFruitNr", plant.gameObject.GetComponent<Plant_Planted>().Saving_ReturnFruitNr(), "plants.es3");
And ensure you specify the same keys when you're loading.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Re: Saving a list of gameobjects with only floats gives errors

Post by Recktardeded »

I see, i thought you could save multiple variables with the same key. I understand now, thanks!
Post Reply