Displaying current player scores in correct order

Options
hi guys,

I'm trying to set up a game over menu that displays the current scores of the players in my Multiplayer game using Photon. so far everything works perfectly with the exception of the order being incorrect. I've tried every thing from Linq to what I currently have now:
using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MULeaderboardsSCORE : MonoBehaviour {
     
     public GameObject leaderboardEntryPrefab;
     public Transform leaderboardgrid;
     public static string usernameString, facebookID = "";
     public List<GameObject> entries = new List<GameObject>();
 
     public void GetLeaderboard()
     {
         for (int i = 0; i < entries.Count; i++) {
             Destroy (entries [i]);
         }
         entries.Clear ();
     
         foreach (PhotonPlayer entry in PhotonNetwork.playerList) {
             GameObject leaderboardEntry = Instantiate (leaderboardEntryPrefab);
             leaderboardEntry.transform.SetParent (leaderboardgrid, false);
             leaderboardEntry.GetComponent<LeaderboardEntry> ().rankString = (entries.Count + 1).ToString ();
             leaderboardEntry.GetComponent<LeaderboardEntry> ().usernameString = entry.customProperties ["username"].ToString ();
             leaderboardEntry.GetComponent<LeaderboardEntry> ().scoreString = entry.customProperties ["score"].ToString ();
             leaderboardEntry.GetComponent<LeaderboardEntry> ().facebookID = entry.customProperties ["FBImage"].ToString ();
             entries.Add (leaderboardEntry);
             Debug.Log ("Leaderboard Updated");
         }
         if (entries.Count > 0) {
             entries.Sort (delegate(GameObject b, GameObject a) {
                 return (a.GetComponent<LeaderboardEntry> ().scoreInt).CompareTo (b.GetComponent<LeaderboardEntry> ().scoreInt);
             });
             Debug.Log ("Leaderboard Sorted");
         }
     }
 }

can anyone tell me what I did wrong and why this script has no effect on the order of my players listed in the game over leaderboards? thanks in advance!!

Comments

  • Tobias
    Options
    In your code, there is no sorting at all. The playerList is ordered by ID. By order of joining the room.
    I guess you want to sort by score?

    Ignoring Linq, I would probably create a list of PhotonPlayer objects "sortedPlayers". Go through the playerList and per player: Go through the indexes of the sortedPlayer list until the current score is no longer less than the index's score.

    Finally, add the LeaderboardEntry one by one by going through the sortedPlayers list.

    By the way: You don't need a custom property for the "username". This is the PhotonPlayer.name (easily set via PhotonNetwork.playerName).
  • naltradamus
    Options
    Hey Tobias,

    thanks for the response. would you by any chance have a reference I could look at? I have a general idea what I should do just not sure how to write that out... so far I have two list, one creating the sorted players and one to instantiate the new entries after the sorted players list has been updated with the new scores?
  • Tobias
    Options
    By what do you sort the players?
    Sorry, I don't have code for this.
  • AniProGuy
    AniProGuy
    edited April 2017
    Options
    I thought that the order of PhotonNetwork.playerList is just random?
    It is definetly not equal for every client in a room.