Loading tons of vars instead of a complete script or GO

Discussion and help for Easy Save 3
Post Reply
Galder
Posts: 3
Joined: Fri Jan 15, 2021 9:10 am

Loading tons of vars instead of a complete script or GO

Post by Galder »

Hi!
I have some doubts about how to load correctly my vars. I had a deep look into the docs and my general load scripts looks like this:

Code: Select all

if(ES3.FileExists()) 
{
	if(ES3.KeyExists("myKey"))
		myVar = ES3.Load<type>("myKey");
}

Ok, that should be correct. But the thing is, I have not only 3-4 vars to load, I have much more. So I Tried this:

Code: Select all

if(ES3.FileExists()) 
{
	try 
	{
		myVar1 = ES3.Load<type>("myKey1");
		myVar2 = ES3.Load<type>("myKey2");
		//...
	}
	catch(KeyNotFoundException e) 
	{
		Debug.Log("error key not found: "+e);
	}
}
I have try that because i believe its not a good idea maken an IF for every key or var i want to load. So the problem comes when every key that not exists the first time I run the game, it thrown a key not found exception and the catch get it, but it stop loading the other variables. Thats the reason I have to check before every single var with an IF. In terms of optimizing the game, it´s not correct at all. Any ideas or suggest??

Thanks for reading!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading tons of vars instead of a complete script or GO

Post by Joel »

Hi there,

The Load methods have a defaultValue parameter which allows you to return a value if the data doesn't exist.

If all of your keys are going to exist, then you can simply use ES3.FileExists before loading them, rather than using ES3.KeyExists for each individual key.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Galder
Posts: 3
Joined: Fri Jan 15, 2021 9:10 am

Re: Loading tons of vars instead of a complete script or GO

Post by Galder »

Hey! thanks for the quick reply!

Not all the variables are going to exists, at least at the begining of the game or mid game. So thats the reason I must check every key instead of using FileExists.
On the other hand, the defaultvalue override method, (Load<type>("key", 0)) doesn´t load the default value if the key does not exist, it throw a key not found exception, -dont know why- because the key exists indeed- thats why I have to use first the try/catch,...to see whats happening.

I´ve tried to save the entire script, but the value is always null or empty:

Code: Select all

"talents_turret" : {
		"__type" : "TalentTurretManager,Assembly-CSharp",
		"value" : {
		"_ES3Ref" : "7011165378132024399",
		"goID" : "4814225795558409998"
	}
There are tons of var in there, but no one is saved. I have to tell that other classes saved in the same way, works fine, but not this.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading tons of vars instead of a complete script or GO

Post by Joel »

Hi there,

Please could you show me how you're saving the entire script, and also the script you're trying to save?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Galder
Posts: 3
Joined: Fri Jan 15, 2021 9:10 am

Re: Loading tons of vars instead of a complete script or GO

Post by Galder »

Yep.

The script to save

Code: Select all

using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System;


[ES3Serializable]
public class TalentTurretManager : MonoBehaviour
{

    public bool doubleCannon; //
    public bool reloadTime; //done
    public bool autoClick; //done
    public bool bossBurst; //done
    public bool enemiesBurst; //done
    public bool efficiency; //done
    public bool criticChance; //done
    public bool criticMultiplier; //done
    public bool armorDestroyer; //done
    public bool holograms; //ultimate
    public bool pet;

    public int piecesAvailable;
    public TextMeshProUGUI piecesAvailableText;
    public int allPiecesGained;
    public int allPiecesWasted;

    public int pointsAvailable;
    public TextMeshProUGUI pointsAvailableText;
    public int allPointsGained; 
    public int allPointsWasted;

    //CHARACTER LEVEL
    public int level;

    //PET TYPES
    public bool bat;
    public bool demon;
    public bool fairy;
    public bool chul;
    public bool petIsInvoked;

    //PET VARS
    public float gold_plus;
    public double extra_dmg;
    public double health_plus;
    public double health_regen_plus;
    public float active_seconds_plus;



    public List<Button> Tier1;
    public List<Button> Tier2;
    public List<Button> Tier3;
    public List<Button> Tier4;
    public List<Button> Tier5;
    public List<Button> TierPet;

    public List<Button> allButtons;

    private readonly float colorUnnactive = .07f;
    private readonly float colorActive = 1f;
    private readonly int unlock = 10;

    //INFOGUNNER VARS
    public float normal_enemies_dmg;
    public float armored_enemies_dmg;
    public float damage_against_bosses;
    public float double_shoot_proc;
    public float force_field_dmg;

    #region GET/SETS
    public float GoldPlus { get { return gold_plus; } set { gold_plus = (float)Math.Round(value, 3, MidpointRounding.AwayFromZero); } }
    public double ExtraDamage { get { return extra_dmg; } set { extra_dmg = value; } }
    public double HealthPlus { get => health_plus; set { health_plus = value; } }
    public double HealthRegenPlus { get => health_regen_plus; set { health_regen_plus = value; } }
    public float ActiveSecondsPlus { get => active_seconds_plus; set { active_seconds_plus = value; } }


    //INFOGUNNER METHODS
    public float NormalEnemiesDmg { get { return normal_enemies_dmg; } set { normal_enemies_dmg = value; } }
    public float ArmoredEnemiesDmg { get { return armored_enemies_dmg; } set { armored_enemies_dmg = value; } }
    public float DamageAgainsBosses { get { return damage_against_bosses; } set { damage_against_bosses = value; } }
    public float DoubleShootProc { get { return double_shoot_proc; } set { double_shoot_proc = value; } }
    public float ForceFieldDmg { get { return force_field_dmg; } set { force_field_dmg = value; } }

    //..

    public bool DoubleCannon { get { return doubleCannon; } set { doubleCannon = value; } }
    public bool AutoClick { get { return autoClick; } set { autoClick = value; } }
    public bool ReloadTime { get { return reloadTime; } set { reloadTime = value; } }
    public bool BossBurst { get { return bossBurst; } set { bossBurst = value; } }
    public bool EnemiesBurst { get { return enemiesBurst; } set { enemiesBurst = value; } }
    public bool Efficiency { get { return efficiency; } set { efficiency = value; } }
    public bool CriticChance { get { return criticChance; } set { criticChance = value; } }
    public bool CriticMultiplier { get { return criticMultiplier; } set { criticMultiplier = value; } }
    public bool ArmorDestroyer { get { return armorDestroyer; } set { armorDestroyer = value; } }
    public bool Holograms { get { return holograms; } set { holograms = value; } }
    public bool Pet { get { return pet; } set { pet = value; } }


    public int PiecesAvailable { get { return piecesAvailable; } set { piecesAvailable = value; } }
    public int PiecesWasted { get { return allPiecesWasted; } set { allPiecesWasted = value; } }
    public int PiecesGained { get { return allPiecesGained; } set { allPiecesGained = value; } }

    public int PointsAvaliable { get { return pointsAvailable; } set { pointsAvailable = value; } }
    public int AllPointsGained { get { return allPointsGained; } set { allPointsGained += value; } }
    public int AllPointsWasted { get { return allPointsWasted; } set { allPointsWasted += value; } }

    public int MyLevel { get { return level; } set { level = value; } }
    #endregion

    public static TalentTurretManager Instance;

    void Awake()
    {
        if(Instance == null)
            Instance = this;
        CheckForLoad();


    }



    public void CheckForLoad()
    {

        if (ES3.FileExists())
        {
            try
            {
                Debug.Log("FILE EXISTS?: " + ES3.FileExists());
                #region POINTS
                if (ES3.KeyExists("gunner_talentos_pointsavailable"))
                    pointsAvailable = ES3.Load<int>("gunner_talentos_pointsavailable");
                if (ES3.KeyExists("gunner_talentos_allpointswasted"))
                    allPointsWasted = ES3.Load<int>("gunner_talentos_allpointswasted");
                if (ES3.KeyExists("gunner_talentos_level"))
                    level = ES3.Load<int>("gunner_talentos_level");
                #endregion

                #region LOAD BOOLS
                if (ES3.KeyExists("gunner_talentos_enemiesburst"))
                    enemiesBurst = ES3.Load<bool>("gunner_talentos_enemiesburst");
                if (ES3.KeyExists("gunner_talentos_armordestroyer"))
                    armorDestroyer = ES3.Load<bool>("gunner_talentos_armordestroyer");
                if (ES3.KeyExists("gunner_talentos_bossburst"))
                    bossBurst = ES3.Load<bool>("gunner_talentos_bossburst");
                if (ES3.KeyExists("gunner_talentos_criticchance"))
                    criticChance = ES3.Load<bool>("gunner_talentos_criticchance");
                if (ES3.KeyExists("gunner_talentos_efficiency"))
                    efficiency = ES3.Load<bool>("gunner_talentos_efficiency");
                if (ES3.KeyExists("gunner_talentos_autoclick"))
                    autoClick = ES3.Load<bool>("gunner_talentos_autoclick");
                if (ES3.KeyExists("gunner_talentos_criticmultiplier"))
                    criticMultiplier = ES3.Load<bool>("gunner_talentos_criticmultiplier");
                if (ES3.KeyExists("gunner_talentos_doublecannon"))
                    doubleCannon = ES3.Load<bool>("gunner_talentos_doublecannon");
                if (ES3.KeyExists("gunner_talentos_reloadtime"))
                    reloadTime = ES3.Load<bool>("gunner_talentos_reloadtime");
                if (ES3.KeyExists("gunner_talentos_ultimate"))
                    holograms = ES3.Load<bool>("gunner_talentos_ultimate");
                if (ES3.KeyExists("gunner_talentos_petisinvoked"))
                    petIsInvoked = ES3.Load<bool>("gunner_talentos_petisinvoked");
                if (ES3.KeyExists("gunner_talentos_bat"))
                    bat = ES3.Load<bool>("gunner_talentos_bat");
                if (ES3.KeyExists("gunner_talentos_pet"))
                    pet = ES3.Load<bool>("gunner_talentos_pet");
                #endregion

                #region FLOATS
                if (ES3.KeyExists("gunner_talentos_damage_against_bosses"))
                    damage_against_bosses = ES3.Load<float>("gunner_talentos_damage_against_bosses");

                if (ES3.KeyExists("gunner_talentos_double_shoot_proc"))
                    double_shoot_proc = ES3.Load<float>("gunner_talentos_double_shoot_proc");

                if (ES3.KeyExists("gunner_talentos_normal_enemies_dmg"))
                    normal_enemies_dmg = ES3.Load<float>("gunner_talentos_normal_enemies_dmg");

                if (ES3.KeyExists("gunner_talentos_armored_enemies_dmg"))
                    armored_enemies_dmg = ES3.Load<float>("gunner_talentos_armored_enemies_dmg");

                if (ES3.KeyExists("gunner_passives_goldplus"))
                    gold_plus = ES3.Load<float>("gunner_passives_goldplus"); //PASSIVES

                if (ES3.KeyExists("gunner_passives_extradmg"))
                    extra_dmg = ES3.Load<double>("gunner_passives_extradmg"); //PASSIVES

                #endregion

                Debug.Log("Que pasa aqui que no carga");
            }

            catch (KeyNotFoundException e)
            {
                Debug.Log("Key not found in savefile: " + e);
            }



        }
    }

    void Start()
    {
        
        // Por defecto dejamos todos los botones apagados
        if (allPointsWasted < 1 && !armorDestroyer && !enemiesBurst)
        {
            foreach (Button but in allButtons)
            {
                Color temp = but.GetComponent<Image>().color;
                temp.a = colorUnnactive;
                but.GetComponent<Image>().color = temp;
                but.enabled = false;
            }

        }
    }





    private void Update()
    {
        if (pointsAvailable < 0) pointsAvailable = 0;
        pointsAvailableText.text = "Points Available " + pointsAvailable;

        CheckUnlockTier1();
        CheckUnlockTier2();
        CheckUnlockTier3();
        CheckUnlockTier4();
        CheckUnlockTier5();
        CheckUnlockTierPet();


    }
And this is how I save from a delegate in Start Method:

Code: Select all

Load_Save_Manager.Save_Obj_Talent_Gunner("talentos_gunner", gunner_talents);
        Load_Save_Manager.InsertFromCache_ToFile();
And the Manager method:

Code: Select all

public static void Save_Obj_Talent_Gunner(string key, TalentTurretManager data)
    {
        ES3.Save(key, data, cache);
    }
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading tons of vars instead of a complete script or GO

Post by Joel »

Hi there,

It looks like it's trying to save it by reference rather than by value. Please could you create a new project with a very basic scene which replicates this and private message it to me?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply