Home Technical Talk

Leader board implementation inside Unity

polycounter lvl 7
Offline / Send Message
salimmatta polycounter lvl 7
Hello guys,

I have created a 2D game, regardless I'm not a developer, but I have followed a tutorial and so far i'm doing pretty well. My game is almost done let's say, even inside the tutorial the language was C# and again i'm learning the basic things when it comes to coding.

However, what concerns me right now is how to implement a leader board inside my game? Because you know, the game would be really useless without a leader board.

Anyone might help me by sharing some techniques, maybe some tutorial or anything?

Thank you ,
Salim

Replies

  • kio
    Options
    Offline / Send Message
    kio polycounter lvl 15
    hm there are quite a few topics involved here..

    serializing & storing the data - that is actually pretty easy with the .net xml stuff, but you'll need to read quite a few docs for this. In the end you want to define a simple data structure which contains your leaderboard entry and throw that into a list/array.

    pseudo c#:

    [System.Serializable]
    Class ScoreItem
    {
     public string name;
     public int score;
    }

    list<ScoreItem>{
    {
      name: foo;
      score: 123;
    }
    1:
    {
     name: bar;
     score: 99;
    }
    }

    once you can read/write this data structure youll have to load it on startup, update & store the list after each level end or whatever.

    yeah and you'll have to create the ui code for this, to actually show the data list. In my mind the list entry is a prefab/part_of_a_screen which you just grab as a 'definition', loop over alle the items in the list, and instantiate
    a list entry item - which you place into a scrollview. I think there are some layout components in unity which do all the placement..

    for item in scores:
        entry  = create_entry(item.name, item.score);
        scrollview.append(entry);

    In programming knowing the right terms to search for really helps... so this should get you started.








  • .Wiki
    Options
    Offline / Send Message
    .Wiki polycounter lvl 8
    If you don´t want to struggle with reading and writing xml files you could store it in the player preferences.

    But there you can only store one key and value. But you can serialize values "manually" to a string and store multiple values (strings, ints) in one value.

    I did this recently to store multiple string values as a list in the player preferences. 

    The script below is for storing and removing favorites. It stores multiple strings that belong to one entry.

    </code><pre class="CodeBlock"><code>void Awake()
        {
            ins = this;
            if (!PlayerPrefs.HasKey("Favorites"))
            {
                PlayerPrefs.SetString("Favorites", null);
            }
        }
    
        public void SetFavorites(string article, string keyName, int index)
        {
            string newFav = "";
            var contains = false;
            var newValue = article + "," + keyName + "," + index.ToString();
            var currentFav = PlayerPrefs.GetString("Favorites");
            var currentFavList = currentFav.Split(';');
            if (currentFav == "")
            {
                newFav = newValue;
                added.SetTrigger("Popup");
            }
            else
            {
                foreach(var value in currentFavList)
                {
                    if(value == newValue)
                    {
                        contains = true;
                    }
                }
    
                if(contains != true)
                {
                    newFav = currentFav + ";" + newValue;
                    added.SetTrigger("Popup");
    
                }
                else
                {
                    foreach(var value in currentFavList)
                    {
                        if(value != newValue)
                        {
                            newFav = newFav + ";" + value;
                        }
    
                    }
                    removed.SetTrigger("Popup");
                    contains = false;
                }         
            }
            PlayerPrefs.SetString("Favorites", newFav.TrimStart(';'));
        }
    
        public void ClearFavorites()
        {
            PlayerPrefs.DeleteAll();
        }




  • salimmatta
    Options
    Offline / Send Message
    salimmatta polycounter lvl 7
    .Wiki said:
    If you don´t want to struggle with reading and writing xml files you could store it in the player preferences.

    But there you can only store one key and value. But you can serialize values "manually" to a string and store multiple values (strings, ints) in one value.

    I did this recently to store multiple string values as a list in the player preferences. 

    The script below is for storing and removing favorites. It stores multiple strings that belong to one entry.

    </code><pre class="CodeBlock"><code>void Awake()
        {
            ins = this;
            if (!PlayerPrefs.HasKey("Favorites"))
            {
                PlayerPrefs.SetString("Favorites", null);
            }
        }
    
        public void SetFavorites(string article, string keyName, int index)
        {
            string newFav = "";
            var contains = false;
            var newValue = article + "," + keyName + "," + index.ToString();
            var currentFav = PlayerPrefs.GetString("Favorites");
            var currentFavList = currentFav.Split(';');
            if (currentFav == "")
            {
                newFav = newValue;
                added.SetTrigger("Popup");
            }
            else
            {
                foreach(var value in currentFavList)
                {
                    if(value == newValue)
                    {
                        contains = true;
                    }
                }
    
                if(contains != true)
                {
                    newFav = currentFav + ";" + newValue;
                    added.SetTrigger("Popup");
    
                }
                else
                {
                    foreach(var value in currentFavList)
                    {
                        if(value != newValue)
                        {
                            newFav = newFav + ";" + value;
                        }
    
                    }
                    removed.SetTrigger("Popup");
                    contains = false;
                }         
            }
            PlayerPrefs.SetString("Favorites", newFav.TrimStart(';'));
        }
    
        public void ClearFavorites()
        {
            PlayerPrefs.DeleteAll();
        }




    Thank you man, but excuse my ignorance about coding. Where exactly should I put the code you gave me? into my player gameobject?
  • .Wiki
    Options
    Offline / Send Message
    .Wiki polycounter lvl 8
    The code can be anywhere in the scene. You can access the whole script when you make it public. But you need to adjust the code. You can´t use it like it is.
Sign In or Register to comment.