- Home /
Trying to save/load game, codes doesnt do anything. not even an error.
UnityScript is my language of choice.
Im trying to write a save/load function, but when i type what ive seen everyone else type it doesnt work, no saving, no loading, it doesnt debug.log and it doesnt error. ive been thinking on this for a few hours and still dont know where to even begin. any thoughts?
and please write in a way thats easy to understand, i have a rough time understanding others. (thats why i STILL cant understand the tutorials i read) replies, especially explaining how code works should be simple or "idiot proof" if you will..
Please and thank you.
this is my code (excluding most things unrelated to the question)
I am trying to copy my variables values and paste into playerprefs i think.. and then call the values from the playerprefs later/after restarting the application. however neither happen and as i said before, no errors in the compiler.
public var playerCurHp : float;
public var playerMaxHp : float;
public var playerCurMp : float;
public var playerMaxMp : float;
public var playerCurExp : float;
public var playerMaxExp : float;
public var playerLevel : float = 1;
public var str : float = 1;
public var atk : float = 1;
public var def : float = 1;
public var vit : float = 1;
public var mag : float = 1;
playerCurHp = 100;
playerMaxHp = 100;
playerCurMp = 100;
playerMaxMp = 100;
playerCurExp = 1;
playerMaxExp = 20;
regenTime = Time.time;
DontDestroyOnLoad(this.gameObject);
if(Input.GetButtonUp("Jump")){
PlayerPrefs.SetFloat("playerLevel", playerLevel);
PlayerPrefs.SetFloat("playerMaxExp", playerMaxExp);
PlayerPrefs.SetFloat("playerCurExp", playerCurExp);
PlayerPrefs.SetFloat("playerMaxHp", playerMaxHp);
PlayerPrefs.SetFloat("playerCurHp", playerCurHp);
PlayerPrefs.SetFloat("playerMaxMp", playerMaxMp);
PlayerPrefs.SetFloat("playerCurMp", playerCurMp);
Debug.Log("1");
}
if(Input.GetButtonUp("MiddleClick")){
playerCurExp = PlayerPrefs.GetFloat("playerLevel");
playerCurExp = PlayerPrefs.GetFloat("playerMaxExp");
playerCurExp = PlayerPrefs.GetFloat("playerCurExp");
playerCurExp = PlayerPrefs.GetFloat("playerMaxHp");
playerCurExp = PlayerPrefs.GetFloat("playerCurHp");
playerCurExp = PlayerPrefs.GetFloat("playerMaxMp");
playerCurExp = PlayerPrefs.GetFloat("playerCurMp");
Debug.Log("2");
}
Answer by Screenhog · Aug 25, 2012 at 03:39 PM
Well, you have the same code twice for GetInt("PlayersCurExp"), and I don't think you're setting the correct values either. But the big problem is probably the fact that you're setting PlayersCurExp and PlayersMaxExp as Floats, and then retrieving them as Ints. If you SetFloat, you should GetFloat, not GetInt.
omg...wow.. ok im sorry i wasted your time like that.. it was actually very late. and i feel kinda stupid now lol. thanks for the input.. ill try again.
ok i edited the code above , now it loads my playersCurExp and players$$anonymous$$axExp it just doesnt load it correctly. ins$$anonymous$$d...i go change my Exp on purpose(to notice if it loads) then i press load after and it loads but without the previous state of exp. ins$$anonymous$$d exp just goes empty(i leveled up again). or it gives a random amount of exp. even if i saved with 80% exp on my exp bar. the level never gets loaded, nor the hp or mp, but exp does seem to change.(to not having any exp)
Read the last bit of code again... you're assigning playerCurExp seven times. :)
(Don't worry about it... I used to make errors like this all the time)
Your answer
Follow this Question
Related Questions
player prefs, problem but no problem??? 1 Answer
playerprefs problem 0 Answers
Save/Load - Strange behaviour 1 Answer
Save and load data with Javascript... 0 Answers