Hello peeps,
I have a quick question, could someone point me to a good tut on local high scores, I see alot of post about them but just bits and pieces of code all I am looking for is how to implement the board, been reading on PlayerPrefs and that is suppose to be used but not understanding it.
Thanks.
public void AddLocalScore() {
string difficulty = GetDifficulty();
string scoreKey = "HScore"+difficulty;
string scoreNameKey = "HScoreName"+difficulty;
string playerNameKey = "PlayerName";
int newScore = myScore;
string newName = PlayerPrefs.GetString(playerNameKey);
int oldScore;
string oldName;
for (int i=0; i<10; i++) {
if (PlayerPrefs.HasKey(scoreKey+i)) {
if (PlayerPrefs.GetInt(scoreKey+i) < newScore) {
// new Score is higher than the stored score
oldScore = PlayerPrefs.GetInt(scoreKey+i);
oldName = PlayerPrefs.GetString(scoreNameKey+i);
PlayerPrefs.SetInt(scoreKey+i,newScore);
PlayerPrefs.SetString(scoreNameKey+i,newName);
newScore = oldScore;
newName = oldName;
}
}
else {
PlayerPrefs.SetInt(scoreKey+i,newScore);
PlayerPrefs.SetString(scoreNameKey+i,newName);
newScore = 0;
newName = "";
}
}
}
Well obviously you will have to modify it a little bit, but that’s how to record a new score.
string scoreKey = "HScore"+GetDifficulty();
string scoreNameKey = "HScoreName"+GetDifficulty();
string m_leaderBoardText = "\n \t Local Top 10: \n\n";
for (int i=0; i<10; i++) {
m_leaderBoardText+= "\t"+(i+1)+". "+ PlayerPrefs.GetInt(scoreKey+i)+ " - ";
m_leaderBoardText+= " "+PlayerPrefs.GetString(scoreNameKey+i)+ " \n";
}
Debug.Log(m_leaderBoardText); // do what ever you want with the string
That’s an example to read datas and display it, in the console here, but you could display it on something else.
Hope it helped.
Thanks for the reply my friend about to jump on it and use this.
Alrighty then, thought I had a handle on this and now I am confused. When my timer stops and the game ends I have called the function for addLocalHighScores, and got an error, not a build error but my time went from 00:00 to -01:-23? IKR, but my question is how do I get it to straight to an input function to add the persons name nest to his/her score. I am coming from XNA so it is kind of confusing of actually doing this in Unity. I have been messing with Unity but never add a score board to it.
Question about highscore again and hopefully someone understands this better than me. I created a scene where the highscore would be displayed but when I try to call it my highscores are all zeroed out like they are not saved for some reason. I put to a score object in the scene so my script could call on it and it does but my scores never carry over or they are not saved as they should be.
any suggestions?
So just to ask one more time about this, is there any more advice or help I could get on this?
Ok I think I have it figured out so in case someone wants to try and use this, in order to setup what I did was to create an overlay gui on top of the current scene, like I would do in XNA just draw a top layer, use that one to display my highscore background and text, I put the AddLocalScore with in the same script and put the:
for (int i=0; i<10; i++) {
m_leaderBoardText+= “\t”+(i+1)+". "+ PlayerPrefs.GetInt(i + scoreKey)+ " - ";
m_leaderBoardText+= " “+PlayerPrefs.GetString(i + scoreNamekey)+ " \n”;
}
in my OnGUI method. Now all I have to do is add the name to it. but in my addlocalscores I put the int scores within the method like so.
AddLocalScores(int scores).
If anyway know how to add names please let me know, still have some work as I am not too familiar with this in Unity.
