Saving a list of gameobjects with only floats gives errors
Posted: Tue Mar 12, 2024 8:15 am
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:
and these are the functions for loading saving (loading get s called on awake)
As a side note, the ProgressFloat gets updated properly, thus its value represents the value needed.
Also, thank you for your time!
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)
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);
}
}
}
Also, thank you for your time!