Best way to create a ranking (leaderboard)
Options
JannickL
✭
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.
Greets
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
0
Comments
-
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>1 -
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 withphotonPlayer.SetCustomProperties(new Hashtable(1) {{"score", score}});
Greets and thank you for the info about how to make a code block0 -
@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}}); }
0 -
@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 } }); }
Ty0 -
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") ; } }
1