Help to create High Score with playerpref for Block breaker

hi, all

i need to know how to create 10 high score(from de biggest to smallest), i ever watch many tutorials but neither show that and i still can’t please help me.

Hello there!

This is how I would do it, it’s not a detailed tutorial, just a rough idea that you can follow.

First we need a list that holds all the scores, I’ll use ints because I’ve never seen a score board with decimals.

List<int> scores = new List<int>(10);

Why not an array? I don’t like arrays when there’s a need to add and remove values, List are far better suited for this kind of job. The list should also be initiated in case there are not enough scores yet, otherwise, when trying to write the score there’ll be an error.

Now it’s all about loops, our friendly loops can help us populate the UI Scoreboard. We’ll need a reference for the Text, we can use multiple texts or just one, depends on what you need, I suggest several, but I’ll show both ways;

With just one Text Component

[Serializefield] Text scoreText;

private void WriteScoreUI()
{
    //Remember there's a list named "scores"
    for (int i = 0; i < scores.Count; i++)
    {
        scoreText.text += scores[i].ToString() + "\n";
    }
}

The “\n” adds an line between each score, I wouldn’t suggest this because it would look weird in different resolutions unless you manage to find a way to keep the text the exact same size regardless of the aspect ratio.

Here’s the loop using a bunch of text components;

[Serializefield] Text[] scoreTexts;

private void WriteScoreUI()
{
    //Remember there's a list named "scores"
    for (int i = 0; i < scores.Count; i++)
    {
        scoreTexts[i].text += scores[i].ToString();
    }
}

Now I used and array, that’s because there’s no need to manipulate it.

With this you can create a functional leaderboard, but now we need to save it, since you asked for PlayerPrefs, I’ll use that

We need to create a key value for each score which is a string, but it has to be different for each. Again, our friendly loops are here to save the day:

private void SaveScores()
{
    for (int i = 0; i < scores.Count; i++)
    {
        PlayerPrefs.SetInt("Score_" + i , scores[i]);
    }

    PlayerPrefs.Save();
}

This will create a bunch of variables that are name “Score_X” (X goes from 0 to 9), and then save them. If the variables already exist this will overwrite them.

That’s it! You have a working leader board! Buuuuut… not really. There’s a huge issue with this code. How are we gonna save the current player score? How do we know if this new score can fit into the leader board? Loops for the… NOPE! We don’t need loops for this, we only need to compare our current score with the lowest score, if it beats it, it gets a place in the leader board.

//This is the player current score.
int currentScore;

private void CompareCurrentScore()
{ 
    if(currentScore > score[scores.Count - 1])
    {
        score[score.Count - 1] = currentScore;
    }
}

This will replace the lowest score if the current score is bigger, But what if the current score topped every other score? Wouldn’t it be weird to appear at the bottom? Of course! That’s why we need to arrange our list.

private void SortScores()
{
    scores.Sort();
    scores.Revers();
}

The Sort method arranges our list but not in the order we want (it sorts it from smallest to biggest int), we need it the other way around, so we just use the Reverse method.

There you go, with this you can create a script that saves, sorts and writes in the UI a leader board.

Unfortunately, copy-pasting this won’t do the work, it’ll almost do it, but you’ll probably want to add a name too, that’s slightly more complicated to pull off.

List<KeyValuePair<string, int>> scores;

This creates a list that holds 2 values instead of one, a string, which should represent the name of the proud player in the leader board and the int which is the score. Of course, if you are using PlayerPrefs you’ll have to save both separately. I’ll leave that up to you.

There’s another issue, How will anyone write a name into the leader board? Well, it depends on how you want to approach this, you can create an old school menu, which I don’t recommend because it’s incredibly complicated, or, you can simply use this:

As I said, this isn’t a copy-paste thing, but I think I gave you everything you need to create your own system.

In the spirit of the GameDev.tv courses, I wish you a happy challenge creating your leader board. If you have more questions please do ask, I’ll be more than glad to answer any question you have.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms