Hello everyone,
I’m programming my fist game and I’d like to learn how to create a scoreboard with C#. For now I’d be happy to have just the local/personal top 3 working, but eventually I’d like to see a board with the top 100 players online.
Basic if statements and simple methods have managed to get me surprisingly far, but for something like this I gather that I ought to look into Arrays or possibly even Lists.
The closest example that I’ve been able to find so far has been in this post [here,] (Add to High Score Array - Questions & Answers - Unity Discussions) specifically
int [] highScore = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int InsertScore( int score ) {
int i = 0;
//Look for the index to insert score
while( i < highScore.length ) {
if( highScore *<= score ) {*
break;
}
i++;
}
//Score doesn’t make it to top 10
if( i >= highScore.length ) {
return -1;
}
//Push all the scores not higher than score backward
for( int j = highScore.length - 1; j <= i; --j ) {
highScore[j] = highScore[j-1];
}
//Set score
highScore = score;
return i;
}
Thank you @Chronos-L
I can follow this enough to start implementing something similar in my own code but before attempting this I did a bit more research and read that “Arrays are outdated and Lists are the way to go” so to speak. I have a feeling that Lists might be slightly more flexible when it comes to arranging data partly because they don’t have to have a fixed length.
Would Lists be able to handle this task in a more elegant way, and if so how? Since I’m still struggling with syntax any links or examples you can provide on the subject would be enormously appreciated!
Thank you for taking the time to read this and I look forward to any feedback!
Best regards,
Don
hi,
what you have should work but it is true that list would be easier… and it has functions to sort it and so on… and you don’t need to specify size
using UnityEngine;
using System.Collections.Generic;
public class ScoreManager : MonoBehaviour {
public List<int> scores = new List<int>();
public bool test;
public bool test2;
public void AddScore(int score)
{
scores.Add(score);
//will add your last score if you call it from somewhere else
}
public void SortScore()
{
scores.Sort();
}
void Update()
{
if(test==true)
{
test = false;
AddScore(Random.Range(0, 1000));
}
if(test2 == true)
{
test2 = false;
foreach (int sc in scores)
{
Debug.Log(sc);
}
SortScore();
foreach (int sc2 in scores)
{
Debug.Log(sc2);
}
}
}
the update part is just so you can test it… hit test a few times and then test 2
you will get the unsorted scores in the console - followed by the sorted ones.
so if you ignore the update part you see the power of list…
3 lines only:
declare
add
sort
and there you are
You can do even more advanced stuff with list using System.Linq but no need here.
Lists are great - would recommend using them,
if you want to have a name with your score… you can great a class with a string and int… then make a list of that… and use OrderBy to sort the list of name/score by score… but this is more advanced…
look for OrderBy for system.Linq for this
hope that helps
Ok, this is my solution:
using UnityEngine;
using System.Collections.Generic; // TO ENABLE THE USE OF List<>
public class YoClass: MonoBehaviour
{ public static List<int> top3list;
public static bool sortingStopped; // STOPS THE LOOP
void Start ()
{ top3list = new List<int>();
top3list.Add(0); // QUICK AND DIRTY WAY TO CREATE
top3list.Add(0); // 3 EMPTY "SLOTS"
top3list.Add(0);
sortingStopped = false;
}
void Update()
{ SortTheList(YoGame.currentScore);
}
void SortTheList(int score)
{ if (sortingStopped == false)
{ if (score > top3list[2])
{ top3list[2] = score;
top3list.Sort();
top3list.Reverse();
}
sortingStopped = true;
}
}
}
sorry for the weird formatting; had some troubles with dat
Might not be pretty, but it gets the job done.
Thanks again @Happy-Zomby!
Only thing I did different was to always re-assign the last index in order to keep the List count to 3.
Then I .Reverse(d) after .Sort(ing) to keep the top score at [0]
Hope this helps anyone else looking for a “basic” score board with C#, and if you think of a more efficient way to do the same thing feel free to post your solution!