Best way to create a ranking (leaderboard)

Options
Hey there,
what would you say is the recommended way to create a ranking or score list for a match in a fps to display the kill amount?

This is how i print the whole player list (nothing special). Every character gets destroyed after the health dropped to zero, so i can't save the score in the player scripts. I got a matchController that should save the score (it exists only one time and doesn't get confused when the masterclient switches). A better way would be to set and update a player custom property but i didn't found out how to use an integer for a custom property.
    private void DisplayPlayerList()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            foreach(PhotonPlayer player in PhotonNetwork.playerList)
            {
                print(player.NickName);
            }
        }
    }
Oh and btw: how can display a code like a code in this forum?

Greets :)

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2016
    Options
    Hi @JannickL,

    Yes one way to do it is saving score in player custom properties as follows:
    
    void SetScore(PhotonPlayer photonPlayer, int score) {
        photonPlayer.SetCustomProperties(new Hashtable(1) {{"score", score}});
    }
    
    private void DisplayPlayerList()
        {
            if (Input.GetKeyDown(KeyCode.Tab))
            {
                for(int i=0; i<PhotonNetwork.playerList.Length; i++)
                {
                    PhotonPlayer player =  PhotonNetwork.playerList[i];
                    Debug.LogFormat("Player:{0} Score:{1}", player.NickName,  player.customProperties["score"]);
                }
            }
        }
    To properly format code blocks:
    1. Select all lines of code to be syntax highlighted and then click on the "pilcrow symbol" (¶▾) and choose "Code".
    2. Wrap the code inside pre and code tags: <pre><code class="CodeBlock">//some code</code></pre>
  • JannickL
    Options
    Hi @JohnTube and thank you for the great reply but one question left:

    what if I always want to take the already stored score in the CustomProps and increase the value += 1 ?

    So is there a way to load the score out of the customProps as an integer to increase this int +=1 and safe it again like you said with
    photonPlayer.SetCustomProperties(new Hashtable(1) {{"score", score}});
    Greets and thank you for the info about how to make a code block :)
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2016
    Options
    @JannickL here you go:
    const string playerScoreKey = "score"; 
    
    void InitOrIncrementScore(PhotonPlayer player) {
    int oldValue = -1;
    if (player.CustomProperties !=  null && !player.CustomProperties.ContainsKey(playerScoreKey)){
         oldValue = (int)player.CustomProperties["score"];
    }
    int newValue = oldValue + 1;
    photonPlayer.SetCustomProperties(new Hashtable(1) {{playerScoreKey, newValue}});
    }
  • JannickL
    JannickL
    edited December 2016
    Options
    @JohnTube so this is what i've done but i got the converting error again:

    1. Cannot convert type object to int in (oldValue = player.CustomProperties["score"];)

    and i needed to create a ExitGames.Client.Photon.Hashtable is that correct?
        public void InitOrIncrementScore(PhotonPlayer player)
        {
            int oldValue = -1;
            if (player.CustomProperties != null && !player.CustomProperties.ContainsKey(playerScoreKey))
            {
                oldValue = player.CustomProperties["score"];
            }
            int newValue = oldValue + 1;
    
            PhotonPlayer photonPlayer = player;
            photonPlayer.SetCustomProperties(new ExitGames.Client.Photon.Hashtable(1) { { playerScoreKey, newValue } });
        }

    Ty :)
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    @JannickL

    I forgot to cast:
    int oldValue = (int)player.CustomProperties["score"];

    Yes it is better to use ExitGames.Client.Photon.Hashtable.
  • JannickL
    Options
    Thank you @JohnTube :) i needed to make so more changes because some operators weren't placed correctly but finally it works :) So for everyone who is searching for this system: The following works for me:
    
     private void Start()
        {
            foreach (PhotonPlayer player in PhotonNetwork.playerList)
            {
                if (player.IsLocal)
                {
                    player.SetCustomProperties(new ExitGames.Client.Photon.Hashtable(1) { { playerScoreKey, 0 } });
                }
            }
        }
    
        public void InitOrIncrementScore(PhotonPlayer player)
        {
            int oldValue = -1;
    
            if (player.CustomProperties != null && player.CustomProperties.ContainsKey(playerScoreKey))
            {
                oldValue = (int)player.CustomProperties["score"];
            }
            int newValue = oldValue + 1;
    
            PhotonPlayer photonPlayer = player;
            photonPlayer.SetCustomProperties(new ExitGames.Client.Photon.Hashtable(1) { { playerScoreKey, newValue } });
    
            Debug.Log(player.NickName + player.CustomProperties["score"]);
        } 
    
    
        public void DisplayPlayerList()
        {
            foreach (PhotonPlayer player in PhotonNetwork.playerList)
            {
                playerListText.text += (player.NickName + " Kills: "+  player.CustomProperties["score"] + " \n") ;
            }
        }