How can i save a list of gameobjects?

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

How can i save a list of gameobjects?

Post by Recktardeded »

Hello, i would like to save a list of gameobjects. each gameobject has a es3 gameobject with all the components checked. the loading function is called on awake and the gameobjects load, but it does not load the variables mannualy assigned as a prefab. Also, the gameobject uses a custom script that is dependent on last saved values, including a enum. I attatched the functions of the loading and saving inside the data manager and also the script of the gameobject:
DATA MANAGER
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");
Debug.Log("loaded plant list");
}
public void Garden_AddToPlantsList(GameObject plantToAdd)
{
garden_PlantedPlantsList.Add(plantToAdd);
Debug.Log("added plant");
}
public void Garden_RemoveFromPlantsList(GameObject plantToRemove)
{
garden_PlantedPlantsList.Remove(plantToRemove);
Debug.Log("removed plant");
}
GAMEOBJECT SCRIPT:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.VFX;

public class Garden_Plant_Planted : MonoBehaviour
{
[Header("General")]
public Garden_Plant_Visuals visuals;
public FruitType_SO plant_fruitType_SO;
public string plant_fruitName;
private float growing_timerInitialize = 1f;
private bool istree;
private enum Growing_Phases { Growing, Collecting, Ded }
private Growing_Phases plant_currentPhase;
[Header("Growing")]
private GameObject growing_fruitsMeshesToGrowFirst;
private GameObject growing_fruitsMeshesToGrowSecondary;
public float growing_currentTime { get; private set; }
private bool growing_Finished = false;
[Header("Collecting")]
private float collecting_Lifetime;
private float collecting_MaxAmountGrownFruit;
private float collecting_fruitRegenTimerReference;
private float collecting_fruitRegenTimer;
public bool collecting_canCollect { get; private set; }
public int plant_fruit_grownAmount;

public GameObject fruit_prefabToThrow;
public float fruit_thrownDuration = 2f;
public GameObject fruit_GrownVisualPrefab;
public List<GameObject> fruit_visual;
private int previousElementsToEnable = 0;
[Header("Ded")]
public bool ded_canRemovePlant;
public Material Material_alive;
public Material Material_ded;
public float dedPlantLerpMaterialDuration;
private void Awake()
{
if (visuals == null)
visuals = gameObject.GetComponent<Garden_Plant_Visuals>();
}
private void Start()
{
istree = plant_fruitType_SO.isTree;
}
void Update()
{
switch (plant_currentPhase)
{
case Growing_Phases.Growing:
Growing_Grow();
break;
case Growing_Phases.Collecting:
Collecting_ApplyCollectingProperties();
break;
case Growing_Phases.Ded:
Ded_ApplyDed();
break;
}

collecting_canCollect = plant_fruit_grownAmount > 0;
}
public void InitializePlant(float newVal)
{
growing_currentTime = newVal;
growing_timerInitialize = newVal;
plant_fruitName = gameObject.name;
plant_fruitType_SO = GameManager.Instance.manager_Data.GetFruitType(plant_fruitName);
collecting_Lifetime = plant_fruitType_SO.plant_collect_plantLifeTime;
ded_canRemovePlant = false;
plant_currentPhase = Growing_Phases.Growing;
growing_Finished = false;
fruit_prefabToThrow = plant_fruitType_SO.Fruit_GeneralMesh;
collecting_fruitRegenTimerReference = plant_fruitType_SO.plant_collect_timeToRegenFruit;
collecting_fruitRegenTimer = plant_fruitType_SO.plant_collect_timeToRegenFruit;
growing_fruitsMeshesToGrowFirst = visuals.plant_meshestoGrowFirst;
if (visuals.plant_meshestoGrowSecond != null)
{
growing_fruitsMeshesToGrowSecondary = visuals.plant_meshestoGrowSecond;
}
int maxAmount = plant_fruitType_SO.plant_collect_maxCapacity;
int amountToRemove = GameManager.Instance.manager_Data_Upgradable.Plant_MaxAmoutToRemove;
int amountToAdd = GameManager.Instance.manager_Data_Upgradable.Plant_MaxAmoutToRemove;
collecting_MaxAmountGrownFruit = Random.Range(maxAmount - amountToRemove, maxAmount + amountToAdd);
collecting_MaxAmountGrownFruit = Random.Range(maxAmount - amountToRemove, maxAmount + amountToAdd);
fruit_GrownVisualPrefab = plant_fruitType_SO.Fruit_GeneralMesh;
}
private void Growing_Grow()
{
float scaleFactor = 1f - Mathf.Clamp01(growing_currentTime / Mathf.Max(0.001f, growing_timerInitialize));
if (growing_fruitsMeshesToGrowFirst != null)
{
growing_fruitsMeshesToGrowFirst.transform.localScale = Vector3.Lerp(Vector3.one * 0.25f, Vector3.one, scaleFactor);
}
growing_currentTime -= Time.deltaTime;
if (growing_currentTime <= 0f && !growing_Finished && plant_currentPhase == Growing_Phases.Growing)
{
Growing_FinishGrowing();
}
}
private void Growing_FinishGrowing()
{
if (growing_fruitsMeshesToGrowSecondary != null)
{
growing_fruitsMeshesToGrowSecondary.gameObject.SetActive(true);
}
plant_currentPhase = Growing_Phases.Collecting;
growing_Finished = true;
}
public void Collecting_Collect()
{ //if shake treshold, start making fruit fall(as the shake float increases, the more they start to fall, so at start
// they don t fall until ex:500, but they start faling at every 50 increment, so at 550,600,etc)
plant_fruit_grownAmount--;
GameManager.Instance.manager_Data.Fruit_Amount_Change(plant_fruitType_SO.name, 1);
Collecting_EnableAmountOfVisualFruit();
}
public void Collecting_ApplyCollectingProperties()//grow fruits and check for lifetime
{
collecting_Lifetime -= 1 * Time.deltaTime;
if (collecting_Lifetime > 0)
{
plant_currentPhase = Growing_Phases.Collecting;
}
if (collecting_Lifetime <= 0)
{
collecting_canCollect = false;
plant_fruit_grownAmount = 0;
plant_currentPhase = Growing_Phases.Ded;
}
if (plant_fruit_grownAmount < collecting_MaxAmountGrownFruit)
{
// Decrease the timer by deltaTime
collecting_fruitRegenTimer -= Time.deltaTime;

// Check if the timer has reached zero
if (collecting_fruitRegenTimer <= 0f)
{
// Increase the number of cycles
plant_fruit_grownAmount++;
// Reset the timer to 5 seconds
collecting_fruitRegenTimer = collecting_fruitRegenTimerReference;
Collecting_EnableAmountOfVisualFruit();
}
}
}
private void Collecting_EnableAmountOfVisualFruit()
{
if (plant_fruit_grownAmount > previousElementsToEnable)
{
// add feel feedback for appearing
}

if (plant_fruit_grownAmount < previousElementsToEnable)
{
// Disable the difference between elementsToEnable and previousElementsToEnable
for (int i = plant_fruit_grownAmount; i <= previousElementsToEnable; i++)
{
visuals.fruit_visualToEnable.gameObject.SetActive(false);
if (istree)
{
GameObject newFruit = Instantiate(plant_fruitType_SO.Fruit_GeneralMesh, visuals.fruit_visualToEnable.transform.position, Quaternion.identity);
newFruit.AddComponent<Rigidbody>();
newFruit.GetComponent<Rigidbody>().mass = 2.5f;
}
}
}

previousElementsToEnable = plant_fruit_grownAmount;

// Enable the necessary game objects randomly
if (plant_fruit_grownAmount < visuals.fruit_visualToEnable.Length)
{
// Create a list of indices to shuffle
List<int> indices = new List<int>();
for (int i = 0; i < visuals.fruit_visualToEnable.Length; i++)
{
indices.Add(i);
}

// Shuffle the indices
System.Random rng = new System.Random();
int n = indices.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
int value = indices[k];
indices[k] = indices[n];
indices[n] = value;
}

// Enable the fruits based on the shuffled indices
for (int i = 0; i < plant_fruit_grownAmount; i++)
{
visuals.fruit_visualToEnable[indices].gameObject.SetActive(true);
}
}
}
public void Ded_ApplyDed()
{
if (visuals != null)
{
var tempArrayOfMeshesToDiscolor = visuals.plant_MeshToApplyDedColor;
// Ensure the target material is set exactly when the duration is complete
foreach (GameObject mat_obj in tempArrayOfMeshesToDiscolor)
{
mat_obj.GetComponent<Renderer>().material.color = Material_ded.color;
mat_obj.GetComponent<Renderer>().material.SetFloat("_Smoothness", 0f);
}
for (int i = 0; i < visuals.fruit_visualToEnable.Length; i++)
{
visuals.fruit_visualToEnable.gameObject.SetActive(false);
}
// Set the boolean to true after lerping is complete
ded_canRemovePlant = true;
}
}
public float Plant_ReturnCurrentLifetime()
{
return collecting_Lifetime;
}
}
Post Reply