c# How would I go about doing a High Score Board?
I have been searching for tutorials but I have only been able to find high scores but no high score boards. I was wanting to show the top three high scores in main menu. I am still new to coding and this is the first time I have had to work with something that needed to be saved. Is anyone able to give examples or have any suggestions/tutorials? Any help would be appreciated, thanks in advance!
Answer by Vicarian · Apr 25, 2017 at 06:38 PM
@dgalbraith3 In general, if you want to store a collection of objects, and add to them dynamically, I'd recommend using a List. The code below isn't complete by any means, but should introduce a couple ways of getting to store scores and retrieve them. Just add this script to a GameObject in your Hierarchy and run the game. You'll get output in the console showing the result of the code placed in the Start() method.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class HighScores : MonoBehaviour
{
// You might change your mind as to the number of high scores to show, so make it
// easily configurable
private const int TOP_SCORE_COUNT = 3;
// Store our scores
private List<PlayerScore> m_scores;
// Say you have a Text object in the Hierarchy and drag that text object into this field of the script
[SerializeField] Text m_topScoreLabel;
void Start() {
m_scores = new List<PlayerScore>();
// Populate m_scores with some test data
m_scores.Add(new PlayerScore() { playerName = "Billy", score = 40 });
m_scores.Add(new PlayerScore() { playerName = "Bob", score = 10 });
m_scores.Add(new PlayerScore() { playerName = "Joe", score = 20 });
m_scores.Add(new PlayerScore() { playerName = "Ray", score = 50 });
// Retrieve our scores
PlayerScore[] top3Scores = GetHighScores(TOP_SCORE_COUNT);
// Print out the top 3 scores - you'll be binding to UI elements and want a known
// number of scores, so you may avoid using a loop, however, you must check to be
// sure you have that known number of elements, like the following:
if (top3Scores.Length == TOP_SCORE_COUNT)
{
Debug.Log("Top scores:");
if (m_topScoreLabel)
m_topScoreLabel.text = "First place: " + top3Scores[0].playerName + ", " + top3Scores[0].score + " points";
Debug.Log("First place: " + top3Scores[0].playerName + ", " + top3Scores[0].score + " points");
Debug.Log("Second place: " + top3Scores[1].playerName + ", " + top3Scores[1].score + " points");
Debug.Log("Third place: " + top3Scores[2].playerName + ", " + top3Scores[2].score + " points");
}
// Output should be:
// Top Scores:
// First place: Ray, 50 points
// Second place: Billy, 40 points
// Third place: Joe, 20 points
}
// Lets throw in a reusable bit of code to get a sorted set of top scores, using Linq
PlayerScore[] GetHighScores(int topCount)
{
return m_scores.OrderBy(ps => ps.score).Take(topCount).ToArray();
}
}
// Need some mutable way of tying a score to a player, so using a Dictionary is discouraged.
// Structs to the rescue:
public struct PlayerScore
{
public int score;
public string playerName;
}
Hey, your script is lifesaving, I know it's been almost 2 years but I'm trying to use your script. It works good with the test data. But I can't figure out two things. The first one is about that I need to save the data when my game is over and retrieve it in the menu's scoreboard, but I can't seem to make two scripts out of this one (one for saving, one for retrieving). The other one is, that my score is stored in a variable called calculatedScore from my CalculatedScore$$anonymous$$anager, and the user name would come through an inputfield. It's clear that it'd look like this for me: [SerializeField] InputField inputField;
...
m_scores.Add(new PlayerScore() { playerName = inputField.text, score = CalculatedScore$$anonymous$$anager.calculatedScore });
...
But I also need a method for the inputfield's On End Edit option.
Could you help me with these if you can and if you're still around?
Thank you!!!
Ist old but If i Try it the Apear in the Opposit Like Smalles Number First?! How can i Change it?
Reverse this part:
return m_scores.OrderBy(ps => ps.score).Take(topCount).ToArray();
Jeahh Perfect. Thanks.! In die end i used: "OrderByDescending"
Your answer
Follow this Question
Related Questions
how to save a lot of character data? 1 Answer
Vroid Opening Incorrect Save,Vroid opens the model above the one I click 0 Answers
Problem with saving player data 0 Answers
Race saving best times on different scene 0 Answers
How to stop a countdown timer and disable/stop everything in the scene? 1 Answer