Search Unity

How to make a Leaderboard?

Discussion in 'Scripting' started by CoroKoro, Feb 13, 2017.

  1. CoroKoro

    CoroKoro

    Joined:
    Mar 24, 2015
    Posts:
    21
    Hi all!

    I was wondering - how can I take a global score variable and put it into a file that contains high scores, and then display it? I'm sure it's been done before, but I've never done it and was looking for a means of implementing this into my game.

    Thanks in advance!
     
  2. JGantic

    JGantic

    Joined:
    Feb 13, 2018
    Posts:
    1
    Did you ever receive an answer or find a solution to your question?
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's a few things. Keeping track of a variable, and probably a list of high scores. Saving and loading from disk, and when it comes time to add, compare the newest score to see if it fits in the list. And displaying them, of course.
    Those are some points you could look into to make a system like that work. If you had a specific question from there, you should post what you've tried and explain your question. :)
     
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    As I can see this potentially attracting more visitors in the future I will add my 2 cents.

    The simple way:
    • Create a dictionary which stores the username of the player and their score. (Whatever type their score may be, int, float, TimeSpan etc). It would look something like:
    Code (CSharp):
    1. private Dictionary<string, int> leaderboard = new Dictionary<string, int>();
    • When fetching the Leaderboard, you could then use Linq (or similar) to sort Dictionary and return the results to get the top players.
    Code (CSharp):
    1. List<KeyValuePair<string, int>> entries = leaderboard.ToList();
    2. List<KeyValuePair<string, int>> entriesOrdered = entries.OrderBy(i => i.Value);
    3. // Alternatively, there is OrderByDescending aswell if you would like it
    4. // in reverse order
    • There is a bunch more I could go into but I would be here for probably hours about generics, adding values, display those values on a UI etc etc. This also assumes you only want local leaderboards and not online ones. That's a whole other conversation.
    References:
    Dictionary - https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx
    Dictionary adding - https://stackoverflow.com/questions/1838476/different-ways-of-adding-to-dictionary
    Linq - https://docs.microsoft.com/en-us/do...guide/concepts/linq/getting-started-with-linq
    Linq OrderBy - https://stackoverflow.com/questions/5344805/linq-orderby-descending-query

    Disclaimer:
    Linq is slightly slower performing generally than alternatives, however, I love it as its super quick to develop with and that for me, is more important.

    Another disclaimer:
    Dictionaries do not allow duplicate keys. This means only one username can exist in the leaderboard, if you would like a user to appear multiple time, you might need to use a list instead and use Linq to fetch the correct entries.

    List - https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

    Another another disclaimer:
    This does not go over saving the leaderboard, to start though, you could just export in a plain text format such as XML or JSON while developing. Near the end though you may want to obfuscate the data a little or use BinaryFormatter or something. (Note that nothing is ever truly safe from editing so definitely don't invest all of your resources here)


    Sellout alert:
    I have an upcoming leaderboard assets soon. I will edit this post when it arrives on the store ;)