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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
using Unity.Services.Leaderboards;
using Unity.Services.Leaderboards.Models;
using Newtonsoft.Json;
public class leaderboardManagement : MonoBehaviour
{
public LeaderboardPlayer playerRanking;
public async void AddWin()
{
var playerEntry = await LeaderboardsService.Instance.AddPlayerScoreAsync("Wins", 1);
print(JsonConvert.SerializeObject(playerEntry));
//Failed solution: JsonConvert.PopulateObject(JsonConvert.SerializeObject(playerEntry), playerRanking);
}
}
public class LeaderboardPlayer
{
public string playerId { get; set; }
public string playerName { get; set; }
public string rank { get; set; }
public string score { get; set; }
public string tier { get; set; }
public string updatedTime { get; set; }
}
I want to be able to access each individual value, but I have no idea where to start. Thanks in advance for any advice!
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”.
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.
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
//LeaderboardScores is a stand-in name for whatever you name your object.
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.
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:
var response = await LeaderboardsService.Instance.GetPlayerRangeAsync(LeaderboardId);
foreach (var entry in response.Results)
{
Debug.Log(entry.PlayerName);
}
The LeaderboardEntry objects have a Metadata property if you have requested it - for example by passing “new GetPlayerRangeOptions() { IncludeMetadata = true }” as an option after the leaderboard ID when calling GetPlayerRangeAsync().
Metadata is a bit unique in that it has to be requested explicitly when fetching scores; we don’t automatically return it as it would mean sending extra data in each response and it would have changed the method response coming back from the service from the original behaviour, so it is only returned when explicitly requested.
I have been able to retrieve the metadata with entry.Metadata and I get the following {“team”:“red”} . At the risk of being a nuisance, how would I access the string “red”?