Search Unity

Resolved How can I extract values to use from the data the methods return?

Discussion in 'Leaderboards' started by CoolBeanz7, Apr 11, 2023.

  1. CoolBeanz7

    CoolBeanz7

    Joined:
    Mar 14, 2022
    Posts:
    18
    I have been setting up leaderboards according to the documentation, but I hit a roadblock. I cannot figure out a way to get any actual leaderboard values from the data returned from
    LeaderboardsService.Instance.AddPlayerScoreAsync()

    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Services.Core;
    5. using Unity.Services.Authentication;
    6. using Unity.Services.Leaderboards;
    7. using Unity.Services.Leaderboards.Models;
    8. using Newtonsoft.Json;
    9.  
    10. public class leaderboardManagement : MonoBehaviour
    11. {
    12.  
    13.     public LeaderboardPlayer playerRanking;
    14.  
    15.     public async void AddWin()
    16.     {
    17.         var playerEntry = await LeaderboardsService.Instance.AddPlayerScoreAsync("Wins", 1);
    18.         print(JsonConvert.SerializeObject(playerEntry));
    19.  
    20.         //Failed solution: JsonConvert.PopulateObject(JsonConvert.SerializeObject(playerEntry), playerRanking);
    21.        
    22.     }
    23. }
    24.  
    25. public class LeaderboardPlayer
    26. {
    27.     public string playerId { get; set; }          
    28.     public string playerName { get; set; }
    29.     public string rank { get; set; }
    30.     public string score { get; set; }
    31.     public string tier { get; set; }
    32.     public string updatedTime { get; set; }          
    33. }
    34.  
    I want to be able to access each individual value, but I have no idea where to start. Thanks in advance for any advice!
     
  2. MidnightGameDeveloper

    MidnightGameDeveloper

    Joined:
    Apr 26, 2014
    Posts:
    123
    You can directly access the data without serializing it to json.
    LeaderboardsService.Instance.AddPlayerScoreAsync() returns an Object of type Unity.Services.Leaderboards.Models.LeaderboardEntry.

    For example you can get the value for the playername with "playerEntry.PlayerName".
     
    CoolBeanz7 likes this.
  3. CoolBeanz7

    CoolBeanz7

    Joined:
    Mar 14, 2022
    Posts:
    18
    That worked perfectly, thank you! I can't believe it was that simple.
     
  4. CoolBeanz7

    CoolBeanz7

    Joined:
    Mar 14, 2022
    Posts:
    18
    Just wanted to add to this after some tinkering, all the possible values to get from GetPlayerScoreAsync are:
    .PlayerId
    .PlayerName
    .Rank
    .Score
    .Tier
    .UpdatedTime

    It would be really beneficial to have this somewhere in the documentation for future users.
     
    Last edited: Apr 14, 2023
    ScaryMonsterStudios likes this.
  5. CoolBeanz7

    CoolBeanz7

    Joined:
    Mar 14, 2022
    Posts:
    18
    How can I get the values from GetPlayerRangeAsync?

    Edit:
    I found it after even more experimentation, and it goes as thus. The GetPlayerRangeAsync() function returns an Object of type Unity.Services.Leaderboards.Models.LeaderboardScores, which contains an Array of Unity.Services.Leaderboards.Models.LeaderboardEntry Objects organized by rank. For example, you can access the name of the player in the highest returned rank above you with
    Code (CSharp):
    1. //LeaderboardScores is a stand-in name for whatever you name your object.
    2.  
    3. print(LeaderboardScores.Results[0].PlayerName);
    I once again must stress to the Unity Team how vital it is that this information be available in the Leaderboards documentation. Thank you for your previous answer, as I would not have been able to figure this out had you not paved the way.
     
    Last edited: Apr 14, 2023
  6. MidnightGameDeveloper

    MidnightGameDeveloper

    Joined:
    Apr 26, 2014
    Posts:
    123
    GetPlayerRangeAsync() returns an object of type Unity.Services.Leaderboards.Models.LeaderboardScores which has a member with the name Results. Results is a list of LeaderboardEntries, so you could access the values for example this way:

    Code (CSharp):
    1. var response = await LeaderboardsService.Instance.GetPlayerRangeAsync(LeaderboardId);
    2.  
    3. foreach (var entry in response.Results)
    4. {
    5.     Debug.Log(entry.PlayerName);
    6. }
     
    CoolBeanz7 likes this.
  7. CoolBeanz7

    CoolBeanz7

    Joined:
    Mar 14, 2022
    Posts:
    18
    Almost perfect timing!:) I had just figured this out right as you posted your reply...
     
    MidnightGameDeveloper likes this.