question

Cadrick Loh avatar image
Cadrick Loh asked

How to use player's statistic and leaderboard?

First of all I would like to mention that I'm totally new to PlayFab and I found PlayFab doc is very confusing.

I'm using PlayFab Unity SDK and it has very little sample codes on C# teaching how to use PlayFab. When I go to the API documentation, all I see are those REST codes (I assume). 

So what I've done so far is only managed to Login. After that I have no ideas what can I do and how should I do in order to submit score to leaderboard.

I even noticed there are 3 types of API and they all looks the same to me, I didn't know which one should I use:

- UpdatePlayerStatisticsRequest

- UpdateCharacterStatisticsRequest

- UpdateUserStatisticsRequest

I couldn't find out where is the documentation talk about the differences between Player, Character, and User.

Assuming I'll be using UpdatePlayerStatisticsRequest,

so what should I do? How should the codes look like?


UpdatePlayerStatisticsRequest request = new UpdatePlayerStatisticsRequest()
{
    StatisticName = "Kill",
     Value = score
};

That's syntax error. I don't even know how does PlayFab codes should look like.

PS: I'm coming from Parse and they did a great job on their documentation by explaining every single of their API in details with sample codes. I do hope you guys can improve your doc in the future. Now I even noticed there's no way to highlight codes in this editor.

10 |1200

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

brendan avatar image
brendan answered

Correct - all our service API calls are REST at the base, with the SDKs providing wrappers around that to provide HTTP and JSON functionality. So the REST documentation applies to all our SDKs, and the call patterns are the same throughout - all the SDKs have the same method names and parameters.

For statistics specifically:

UpdatePlayerStatistics: This is for updating statistics which belong to the players account directly, and is the most common use case.

UpdateCharacterStatistics: This is for updating statistics specifically associated with a Character, which is a sub-element of the player account. Think of them as being like character slots in an MMO - they have distinct names, stats, and inventory, but they belong to the player account.

UpdateUserStatistics: This is our older API call, for updating stats for a player. It is replaced by UpdatePlayerStatistics, and will be deprecated soon.

Now since you're using Unity, I would recommend checking our PlayFab-Samples repository, as it shows a number of "recipes" for how to do things in Unity: https://github.com/PlayFab/PlayFab-Samples.

For example, if you have a look at the way UpdateUserStatistics is called (and UpdatePlayerStatistics follows the same model), you'll see that the parameter for input to this function is UserStatistics - a string, int Dictionary. This is because you can pass in many statistics in a single call.

So to make your call, you would actually want to do something like this:

UpdatePlayerStatisticsRequest request = new UpdatePlayerStatisticsRequest();
request.UserStatistics = new Dictionary<string, int>();
request.UserStatistics["Kill"] = score;
PlayFabClientAPI.UpdateUserStatistics(updateRequest, UpdateUserStatsCallback, SharedErrorCallback);

For your other questions:

No, statistics currently cannot be deleted. We will be updating to provide a way to do this later this quarter.

And yes, as stated in the documentation for the statistics update calls, all statistics are leaderboards in PlayFab - that's specifically the design intent, to provide a simple way to create leaderboards. And yes, each statistic can be configured to reset on a pre-defined interval. If your design is to track a score for players, and have it reset on a daily, weekly, and monthly basis, then yes, you would need three separate statistics (and so, leaderboards).

10 |1200

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

Cadrick Loh avatar image
Cadrick Loh answered

Second question: 

I noticed Leaderboard was automatic created based on Statistics. So if I want to have 1 statistic date (for example: Score)

But I need to have 3 types of leaderboard where each of them will reset itself after a time interval. (daily, weekly, and monthly)

So the time when I submit statistics I need to submit 3 types in an API call?

For example, ScoreDaily, ScoreWeekly, ScoreMonthly?

10 |1200

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

Cadrick Loh avatar image
Cadrick Loh answered

Third question:

Is there any way to delete Leaderboard that I've created in Game Manager?

10 |1200

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

Cadrick Loh avatar image
Cadrick Loh answered

Thanks for the reply!
I like PlayFab support!

10 |1200

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

Daniel Kiefer avatar image
Daniel Kiefer answered

UpdatePlayerStatisticsRequest request = new UpdatePlayerStatisticsRequest();
request.UserStatistics = new Dictionary<string, int>();
request.UserStatistics["Kill"] = score;
PlayFabClientAPI.UpdateUserStatistics(updateRequest, UpdateUserStatsCallback, SharedErrorCallback);

this doesn't work for me any more because 

request.UserStatistics does not exist

and 

request.Statistics is a List<StatisticUpdate>

could you explain the workflow there?

 

10 |1200

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

brendan avatar image
brendan answered

That's because you're now using UpdatePlayerStatistics. The UpdateUserStatistics call used a class named UpdateUserStatisticsRequest - hence, the difference in structure.

The UpdateUserStatistics call is being deprecated, in favor of UpdatePlayerStatistics, which provides greater functionality. Older titles can continue to use UpdateUserStatistics, but it is being phased out of the SDKs, so that titles move to the newer API call.

Our apologies that this wasn't better messaged - the tools team just met today, in fact, to review the deprecation process, and all the messaging that needs to go along with it.

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.