question

Charles McGregor avatar image
Charles McGregor asked

How do you get total number of entries in a Leaderboard?

I'm looking to grab the total number of entries a leaderboard has so I can use that number for other features in my game. I'm not looking for the data from all of the entries at once, just the total number of them. I want to have a feature that lets you know if you are in the "Top 50%" or "Top 25%" and so on in a leaderboard to give you some context to how well you did. I also am breaking up the amount of entries shown on the screen into pages and I want to show how many pages there are (ex: Page 12 of 342).

If I can only get a ballpark estimate of how many there are would work ok as well. If there was actually 4,384 entries, but it estimates 4,000 or 5,000 entries that would be fine. I'm trying to let the player know roughly where they land in terms of all other players at least. I also know that things aren't precise after 1,000 entries so if it isn't possible to have more pages than the 1,000 entries allow or I even have to get rid of the concept of pages that is ok as well, just trying to see if it is possible.

unity3dLeaderboards and Statisticslimits
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Citrus Yan avatar image
Citrus Yan answered

Currently PlayFab does not provide such API to do the work directly. However, since Leadboards are based on Statistics, hence we can count the amount of a specific Statistic type possessed by players to estimate the total number of entries in a Leaderboard, here is a workaround you can consider:

  1. Let’s suppose we want to count the number of entries in a Leaderboard for Statistic s, and, the values set for s are greater than 0.
  2. Create a segment for players whose values of Statistic s are great than 0, this will exclude those players who haven’t got into the Leaderboard yet, which gives an estimate number of entries for this Leaderboard:

  3. Call Server/GetPlayersInSegment API to page through all players in that newly created segment, of course we don’t want to actually parse through all the players, instead, what we really want the ProfilesInSegment property which indicates the amount of players in that segment returned from the API call, in other words, the estimate number of total entries for the aforementioned Leaderboard. Therefore, here is sample request:
{

  "SegmentId":
"id_of_the_newly_created_segment", //segmentId can be retrieved using
Server/GetAllSegments

  "MaxBatchSize": 1, //minimum value
to be set, this intends to reduce the overhead cost on the PlayFab service

  "SecondsToLive":1 //minimum value
to be set, this intends to reduce the overhead cost on the PlayFab service

}

    Please also note that the GetPlayersInSegment API is limited to being called 30 times in one minute. You will be returned an error if you exceed this threshold. Therefore, it’s suggested that you cache the value of the total number in Title Data or something similar, for instance, a database hosted by yourself.

    segconfig.png (6.5 KiB)
    4 comments
    10 |1200

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

    Charles McGregor avatar image Charles McGregor commented ·

    Ah ok, this seems like a nice workaround, thanks for the suggestion! Though I do have a question on accessing that server data in a secure way from Unity. I'm pretty new to the PlayFab workflow and I don't know how to access those server API calls safely. Would this be where CloudScripts would come in?

    0 Likes 0 ·
    Citrus Yan avatar image Citrus Yan Charles McGregor commented ·

    You cannot directly call server APIs from the client-side, that's for sure. You'll need to access those server APIs from the server-side, either through your own hosted servers, or CloudScript. CloudScript enables you to build server-side logic and functionality that scales to meet your demand, without worrying about servers or infrastructure, please navigate to this doc for more info: https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript/

    0 Likes 0 ·
    Charles McGregor avatar image Charles McGregor Citrus Yan commented ·

    Ah ok, gotcha. Thanks so much for the clarification!

    0 Likes 0 ·
    Show more comments
    smumair06 avatar image
    smumair06 answered

    Still we don't have such API or is it available now ?

    10 |1200

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

    Write an Answer

    Hint: Notify or tag a user in this post by typing @username.

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.