Search Unity

Question Getting players rank from game center

Discussion in 'iOS and tvOS' started by enhawk, Nov 14, 2021.

  1. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    I’m trying to get the players rank, the only way I can see to do this is to cycle through all scores and find the match. With millions of score entries, this takes ages.

    is there an easier way?
     
    Last edited: Nov 15, 2021
  2. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    heres the code I'm currently trying, it doesn't return a rank.

    Code (CSharp):
    1.  
    2.     public int GetUserRank()
    3.     {
    4.         string user = Social.localUser.id;
    5.         int rank = 0;
    6.         Social.LoadScores("scoreBoardName", scores =>
    7.         {
    8.             if (scores.Length > 0)
    9.             {
    10.                 Debug.Log("Retrieved " + scores.Length + " scores");
    11.  
    12.                 //Filter the score with the user name
    13.                 for (int i = 0; i < scores.Length; i++)
    14.                 {
    15.                     if (user == scores[i].userID)
    16.                     {
    17.                         rank = scores[i].rank;
    18.                     }
    19.                 }
    20.             }
    21.             else
    22.             {
    23.                 Debug.Log("Failed to retrieved score");
    24.             }
    25.         });
    26.         return rank;
    27.     }
    28.  
     
  3. amace444

    amace444

    Joined:
    Jan 16, 2022
    Posts:
    1
    Social.LoadScores runs asynchronously. Meaning, it's still running when you hit your return statement of return rank. If you need the rank, you need to structure your code to use it inside of the LoadScores callback function.