- Home /
Leaderboard Scores only posts highest score?
Hello, So I have made a fully functional Leaderboard System that will display the top 3 scores. The only problem is, is that only the highest score will display.
Example: Name Score Name Score Name Score
Instead of what it is supposed to look like: Name1 Score1 Name2 Score2 Name3 Score3
This is my current code and I am using an IEnumerator to display the leaderboard.
IEnumerator UpdateScore(){
highScore2 = highScore3;
PlayerPrefs.SetString(highScore3TextKey, userName3);
PlayerPrefs.SetInt(highScore3Key, highScore3);
PlayerPrefs.Save();
yield return new WaitForSeconds(1);
highScore1 = highScore2;
PlayerPrefs.SetString(highScore2TextKey, userName2);
PlayerPrefs.SetInt(highScore2Key, highScore2);
PlayerPrefs.Save();
yield return new WaitForSeconds(1);
userHighScore = highScore1;
PlayerPrefs.SetString(highScore1TextKey, userName1);
PlayerPrefs.SetInt(highScore1Key, highScore1);
PlayerPrefs.Save();
Start();
}
What needs to happen is when there is a new highscore, highscore 3 becomes, highscore 2, highscore 2 becomes 1 and highscore1 becomes the new highscore. Thanks if you can help in advanced!
Answer by MaxEden · Feb 11, 2015 at 02:39 PM
Consider this highscore board snippet
class HighscoreBoard
{
public class HighscoreRecord
{
public string Name;
public int Score;
}
public List<HighscoreRecord> Records = new List<HighscoreRecord>();
public void AddRecord(string name, int score)
{
Records.Add(new HighscoreRecord()
{
Name = name,
Score = score
});
Records = Records
//Sort
.OrderByDescending(p => p.Score)
//Take first 3
.Take(3)
.ToList();
string recordsAsString = String.Join(";",
Records
.Select(RecordToString)
.ToArray()
);
PlayerPrefs.SetString("Highscores", recordsAsString);
}
public void ReadHighscores()
{
if (!PlayerPrefs.HasKey("Highscores")) return;
Records = PlayerPrefs.GetString("Highscores")
.Split(';')
.Select(RecordFromString)
.ToList();
}
string RecordToString(HighscoreRecord record)
{
return record.Name + ':' + record.Score;
}
HighscoreRecord RecordFromString(string str)
{
string[] parts = str.Split(':');
return new HighscoreRecord()
{
Name = parts[0],
Score = Int32.Parse(parts[1])
};
}
}
Of course there is a pinch of Linq magic, but it's small and handy.
This works really well, thank you. I had to make some small changes, otherwise it wouldn't compile:
String.Join should be string.Join
Int32.Parse should be int.Parse
and I had to declare the Select method this way:
Select<HighscoreRecord, string>(RecordToString)
Select<string, HighscoreRecord>(RecordFromString)
Otherwise, it works like a charm.
Answer by Dicen777 · Feb 11, 2015 at 02:25 PM
You are essentially assigning highScore3
to all other scores.
EDIT: I think that you are under the assumption that the left gets assigned to the right, which is why the following looks correct. The left is always the updated value and the right is the value that it gets updated to.
highScore2 = highScore3;
highScore1 = highScore2;
userHighScore = highScore1;
You want to reverse the variables in respect to "=". I brought all the assignments up to the top since it will still work and it is easier to understand the change IMO.
IEnumerator UpdateScore(){
highScore3 = highScore2;
highScore2 = highScore1;
highScore1 = userHighScore;
PlayerPrefs.SetString(highScore3TextKey, userName3);
PlayerPrefs.SetInt(highScore3Key, highScore3);
PlayerPrefs.Save();
yield return new WaitForSeconds(1);
PlayerPrefs.SetString(highScore2TextKey, userName2);
PlayerPrefs.SetInt(highScore2Key, highScore2);
PlayerPrefs.Save();
yield return new WaitForSeconds(1);
PlayerPrefs.SetString(highScore1TextKey, userName1);
PlayerPrefs.SetInt(highScore1Key, highScore1);
PlayerPrefs.Save();
Start();
}
Thanks! But still for some reason it still does the same thing. It will update all the names for the score as the same name but it will also just displays the score as 0 for each highscore..
So this code only saves to a file to be accessed later. Can you post the code that retrieves this information and displays the score?
I am saving everything to PlayerPrefs and I am display everything through each .text .
void Start () {
userHighScore = PlayerPrefs.GetInt(highScore$$anonymous$$ey,0);
Initalize();
highScoreNameText1.text = userName1;
highScoreNameText2.text = userName2;
highScoreNameText3.text = userName3;
highScoreText1.text = highScore1.ToString();
highScoreText2.text = highScore2.ToString();
highScoreText3.text = highScore3.ToString();
userHighScore = PlayerPrefs.GetInt(highScore$$anonymous$$ey,0);
}
void Initalize(){
userName1 = PlayerPrefs.GetString(highScore1Text$$anonymous$$ey, "Empty");
userName2 = PlayerPrefs.GetString(highScore2Text$$anonymous$$ey, "Empty");
userName3 = PlayerPrefs.GetString(highScore3Text$$anonymous$$ey, "Empty");
highScore1 = PlayerPrefs.GetInt(highScore1$$anonymous$$ey,0);
highScore2 = PlayerPrefs.GetInt(highScore2$$anonymous$$ey,0);
highScore3 = PlayerPrefs.GetInt(highScore3$$anonymous$$ey,0);
}
I think at this point you want to do some debugging in order to see where things are going wrong. You can use Debug.Log
to do this.
For instance, start with putting a couple lines in the functions that are involved so you know they are getting called. so in UpdateScore()
make the first line
Debug.Log("UpdateScore Called");
once you know that all your functions are getting called correctly, start logging information from those calls like variable values.
Debug.Log(highScore1.ToString());
and see where the value changes from the score to 0. If you can find where that is happening and still can't solve the issue, comment what you found.
One issue (presumably) that I see though is you are only updating the text in the Start()
function, meaning it only gets updated when the attached object is created.
void Start () {
highScoreNameText1.text = PlayerPrefs.GetString(highScore1Text$$anonymous$$ey, "Empty");
highScoreNameText2.text = PlayerPrefs.GetString(highScore2Text$$anonymous$$ey, "Empty"); //displays and retives the highscore texts
highScoreNameText3.text = PlayerPrefs.GetString(highScore3Text$$anonymous$$ey, "Empty");
highScoreText1.text = updateHighScore1.ToString();
highScoreText2.text = updateHighScore2.ToString(); //displays the highscore numbers
highScoreText3.text = updateHighScore3.ToString();
updateHighScore1 = PlayerPrefs.GetInt(highScore1$$anonymous$$ey,0);
updateHighScore2 = PlayerPrefs.GetInt(highScore2$$anonymous$$ey,0); //retrieves the highscore numbers
updateHighScore3 = PlayerPrefs.GetInt(highScore3$$anonymous$$ey,0);
}
void Update () {
if(highScore1 >= userHighScore);{
StartCoroutine(UpdateScore());
userName = PlayerPrefs.GetString(userHighScoreTextSave, "Empty"); //grabs the usernname text
userHighScore = PlayerPrefs.GetInt(highScore$$anonymous$$ey,0); //grabs the new highscore
}
}
IEnumerator UpdateScore(){
highScore2 = highScore3;
highScore2 = highScore1;
highScore1 = userHighScore;
userName2 = userName3;
userName2 = userName1;
userName1 = userName;
//sets highscore 3
PlayerPrefs.SetString(highScore3Text$$anonymous$$ey, userName3);
PlayerPrefs.SetInt(highScore3$$anonymous$$ey, highScore3);
PlayerPrefs.Save();
yield return new WaitForSeconds(1);
//sets highscore 2
PlayerPrefs.SetString(highScore2Text$$anonymous$$ey, userName2);
PlayerPrefs.SetInt(highScore2$$anonymous$$ey, highScore2);
PlayerPrefs.Save();
yield return new WaitForSeconds(1);
//sets highscore 3
PlayerPrefs.SetString(highScore1Text$$anonymous$$ey, userName1);
PlayerPrefs.SetInt(highScore1$$anonymous$$ey, highScore1);
PlayerPrefs.Save();
Start(); //updates the score
yield return new WaitForSeconds(1);
}
This is my full script, high score 1 and 2 display but like before they display the highest score... High score 3 displays no text and a score of zero. I have have been debugging too.