How can you save custom components of a gameobject aswell?

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

How can you save custom components of a gameobject aswell?

Post by Recktardeded »

Hello, i have a list of gameobjects where i would like to save a custom script aswell(as i ve seen custom scripts are not available to be saved, only unity built in components/scripts). I was thinking i could save just a few floats necesarry to the gameobject, but maybe saving the script itself would be quicker/easier to manage.I wanted to save the enum, and a few floats, but i do not know how to manage the saving/loading for all the elements inside the list. Here is the custom script for reference:
{
public FruitType_SO plant_fruitType_SO;
[field:Header("Growing")]
private bool growing_Finished = false;
public float growing_currentTime { get; private set; }
private GameObject[] growing_fruitsMeshesToGrow;
[field: Header("Collecting")]
public bool collecting_plant_hasFruitAvailable{ get; private set; }
private float plantLifeTime;
private int collecting_maxCapacity;
private float fruitRegenTimer = 0.5f;
public int plant_currentFruitAmount { get; private set; }
private float collecting_timeToGrowAnotherFruit;
[field: Header("Growing")]
public bool ded_canRemovePlant;
//misc necesary
private float growing_timerInitialize = 1f;
private enum Growing_Phases
{
Growing,
Collecting,
Ded
}
private Growing_Phases plant_currentPhase;
private void Awake()
{
ded_canRemovePlant = false;
plantLifeTime = 15f;
plant_currentPhase = Growing_Phases.Growing;
growing_Finished = false;
Growing_SelectFruitsToGrow();
}
void Update()
{
switch (plant_currentPhase)
{
case Growing_Phases.Growing:
Growing_IncreaseFruitScale();
break;
case Growing_Phases.Collecting:
{
Collecting_RegenFruits();
plantLifeTime -= 1 * Time.deltaTime;
if(plantLifeTime > 0)
{
plant_currentPhase = Growing_Phases.Collecting;
}
if(plantLifeTime <= 0)
{
collecting_plant_hasFruitAvailable = false;
plant_currentFruitAmount = 0;
plant_currentPhase = Growing_Phases.Ded;
//foreach(GameObject obj in growing_fruitsMeshesToGrow)
//Destroy(obj);
}
}
break;
case Growing_Phases.Ded:
ded_canRemovePlant = true;
break;
}
if (plant_currentFruitAmount > 0)
{
collecting_plant_hasFruitAvailable = true;
}
if (plant_currentFruitAmount <= 0)
{
collecting_plant_hasFruitAvailable = false;
}
}
private void Growing_IncreaseFruitScale()
{
float scaleFactor = 1f - Mathf.Clamp01(growing_currentTime / Mathf.Max(0.001f, growing_timerInitialize));
foreach (GameObject obj in growing_fruitsMeshesToGrow)
{
obj.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.Collecting;
growing_Finished = true;
}
}
private void Growing_SelectFruitsToGrow()
{
growing_fruitsMeshesToGrow = new GameObject[transform.childCount];
// Loop through each child and add it to the array
for (int i = 0; i < transform.childCount; i++)
{
growing_fruitsMeshesToGrow = transform.GetChild(i).gameObject;
transform.GetChild(i).gameObject.transform.localScale = Vector3.zero;
}
}
public void Growth_SetTimer(float newVal)
{
growing_currentTime = newVal;
growing_timerInitialize = newVal;
}
private void Collecting_InitializePlant()
{
string NameOfPlant = gameObject.tag.Substring("Plant_".Length);
plant_fruitType_SO = GameManager.Instance.manager_Data.GetFruitType(NameOfPlant);
}
public void Collecting_Collect()
{
plant_currentFruitAmount--;
Debug.Log(plant_currentFruitAmount);
}
public void Collecting_RegenFruits()
{
if (plant_currentFruitAmount < 10)
{
// Decrease the timer by deltaTime
fruitRegenTimer -= Time.deltaTime;

// Check if the timer has reached zero
if (fruitRegenTimer <= 0f)
{
// Increase the number of cycles
plant_currentFruitAmount++;
// Reset the timer to 5 seconds
fruitRegenTimer = 0.5f;
}
}
}
private void InitializePlantProperties()
{
plantLifeTime = plant_fruitType_SO.plant_collect_LifeTime;
}
}
Thank you for your time!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can you save custom components of a gameobject aswell?

Post by Joel »

Hi there,

This is covered in the Saving and Loading GameObjects guide:
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

Specifically the Choosing which Components are saved section.

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 you save custom components of a gameobject aswell?

Post by Recktardeded »

Thank you for your reply, but could you please let me know what would be the best way to load/save the list? Should i load it on awake, and save it with autosave or when i add/remove gameobjects?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can you save custom components of a gameobject aswell?

Post by Joel »

Hi there,

When you save very much depends on your project, but saving on Awake and whenever the List changes is appropriate.

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 you save custom components of a gameobject aswell?

Post by Recktardeded »

Thank You! It works great, have a great day!
Post Reply