PlayerPrefs saving and Loading
I got this little code. I made it so it'll save the Floats and It's from different values. Now, what am I missing? I'm very Newb'ish when it comes to PlayerPrefs.
using UnityEngine;
using System.Collections;
public class SaveState : MonoBehaviour {
public AgeCounter ageC;
public Click click;
public EXP exp;
public overallPoops poops;
public void Save()
{
PlayerPrefs.SetInt ("Age", ageC.ageNum );
PlayerPrefs.SetInt ("Age2", ageC.targetExp);
PlayerPrefs.SetInt ("GPC", click.goldperclick);
PlayerPrefs.SetInt ("exp", exp.experience);
PlayerPrefs.SetInt ("expPS", exp.experiencePS);
PlayerPrefs.SetFloat ("Gold", click.gold);
PlayerPrefs.SetFloat ("OverallG", click.overalGold );
PlayerPrefs.SetFloat ("GPC", click.goldperclick);
PlayerPrefs.SetFloat ("Health", poops.health);
}
public void Load()
{
int Age = PlayerPrefs.GetInt ("Age", ageC.ageNum);
int Age2 = PlayerPrefs.GetInt ("Age2", ageC.targetExp);
int GPC = PlayerPrefs.GetInt ("GPC", click.goldperclick);
int exp = PlayerPrefs.GetInt ("exp");
int expPS = PlayerPrefs.GetInt ("expPS");
float gold = PlayerPrefs.GetFloat ("Gold");
float overallG = PlayerPrefs.GetFloat ("OverallG");
float gpc = PlayerPrefs.GetFloat ("GPC");
float health = PlayerPrefs.GetFloat ("Health");
}
}
what did i Miss in Loading?
Please, don't make us guess about what the problem is - just tell us. Do you get a compiler error, a runtime error, no error but not proper result, ????
It's straight forward what's the problem though. The Load Section is wrong.
Answer by Jessespike · Apr 13, 2016 at 06:03 PM
The Load function is storing data in local variables, once the Load function completes, all the data you retrieved is lost.
Change int Age = PlayerPrefs.GetInt ("Age", ageC.ageNum);
to ageC.ageNum = PlayerPrefs.GetInt ("Age", 0);
The "0" is the default value to use, if Age doesn't exist in PlayerPrefs.
So, when tehre is NO data then Get(Int,String,Float) will get the default value that i set after the saved PlayerPrefs. Right? For example like you said, default start age is 0, no data yet saved so it'll use the Default value 0 that i set in the Script. If so, your answer gets the top since i allready figured out the loading process.
That is correct. If the PlayerPref cannot be found, then the default value is used ins$$anonymous$$d.
Well. To the Top with you. Here's how I figured everything out with all you guys's help.
Answer by M-Hanssen · Apr 13, 2016 at 06:57 PM
This is correct:
using UnityEngine;
using System.Collections;
public class SaveState : MonoBehaviour
{
public AgeCounter ageC;
public Click click;
public EXP exp;
public overallPoops poops;
public void Save()
{
PlayerPrefs.SetInt("Age", ageC.ageNum);
PlayerPrefs.SetInt("Age2", ageC.targetExp);
PlayerPrefs.SetInt("GPC", click.goldperclick);
PlayerPrefs.SetInt("exp", exp.experience);
PlayerPrefs.SetInt("expPS", exp.experiencePS);
PlayerPrefs.SetFloat("Gold", click.gold);
PlayerPrefs.SetFloat("OverallG", click.overalGold);
PlayerPrefs.SetFloat("GPC", click.goldperclick);
PlayerPrefs.SetFloat("Health", poops.health);
}
public void Load()
{
int Age = PlayerPrefs.GetInt("Age");
int Age2 = PlayerPrefs.GetInt("Age2");
int GPC = PlayerPrefs.GetInt("GPC");
int exp = PlayerPrefs.GetInt("exp");
int expPS = PlayerPrefs.GetInt("expPS");
float gold = PlayerPrefs.GetFloat("Gold");
float overallG = PlayerPrefs.GetFloat("OverallG");
float gpc = PlayerPrefs.GetFloat("GPC");
float health = PlayerPrefs.GetFloat("Health");
}
}
And indeed only use the second parameter if you want to set a default value.
Thanks. however you still need to specify where does the Int and Float go,
something like this
exp.experience = exp;
Are you seriously asking because you don't know? Here it is:
public class SaveState : $$anonymous$$onoBehaviour
{
public AgeCounter ageC;
public Click click;
public EXP exp;
public overallPoops poops;
public void Save()
{
PlayerPrefs.SetInt("Age", ageC.ageNum);
PlayerPrefs.SetInt("Age2", ageC.targetExp);
PlayerPrefs.SetInt("GPC", click.goldperclick);
PlayerPrefs.SetInt("exp", exp.experience);
PlayerPrefs.SetInt("expPS", exp.experiencePS);
PlayerPrefs.SetFloat("Gold", click.gold);
PlayerPrefs.SetFloat("OverallG", click.overalGold);
PlayerPrefs.SetFloat("GPC", click.goldperclick);
PlayerPrefs.SetFloat("Health", poops.health);
}
public void Load()
{
ageC.ageNum = PlayerPrefs.GetInt("Age");
ageC.targetExp = PlayerPrefs.GetInt("Age2");
click.goldperclick = PlayerPrefs.GetInt("GPC");
exp.experience = PlayerPrefs.GetInt("exp");
exp.experiencePS = PlayerPrefs.GetInt("expPS");
click.gold = PlayerPrefs.GetFloat("Gold");
click.overalGold = PlayerPrefs.GetFloat("OverallG");
click.goldperclick = PlayerPrefs.GetFloat("GPC");
poops.health = PlayerPrefs.GetFloat("Health");
}
}