question

activedreaminc avatar image
activedreaminc asked

How to add statistics such as Level, EXP to new player?

Hi, I am having trouble adding statistics that I have defined in Leaderboards such as Level and EXP to new player. When I tried adding the statistics manually from the Game Manager it works but it will be very cumbersome if I have to add the statistics manually to every new player using Game Manager. Is there a way to do this through code? I tried using GetPlayerStatistics and UpdatePlayerStatistics but it's still not working.

Please help me

Thank you

Player DataAccount ManagementLeaderboards and Statisticsgame 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.

brendan avatar image
brendan answered

Any call to update the player statistics would work to do this. So, UpdatePlayerStatistics works fine - I just checked in my own test title. What are the specifics of the call you are making - Title ID, is this Client or Server API, what parameters are you passing in, and what are the details of the error response you get back?

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.

activedreaminc avatar image activedreaminc commented ·
  1. Hi@Brendan, thank you for your reply. Below in the answer are my codes,
  2. myTitle ID is CA1F. The Player Ingrams is the one I already added the statistics manually, however the codes are not working to other players such as Grudiev or any other ones that I have not added the Statistics manually. Also, how to access PlayFabServerAPIorPlayFabAdminAPIfrom C#?
  3. Thank you for your help
0 Likes 0 ·
brendan avatar image brendan activedreaminc commented ·

Thanks, but the main question is still what is the error message you get back from the call to update the player statistics? I notice that you're trying to write to player stats from the Client SDK, which is disabled by default, since that makes it trivially easy for hackers to post any stats they want. Have you enabled that API call via the Settings tab?

As to the Server and Admin API calls, are you using the editor extensions for Unity? There's a checkbox to enable each API in the top-level dashboard of that tool. Otherwise, the APIs are all included in the SDK you can get from GitHub - you just need to set the defines to allow the Server and Admin SDKs to be used in your project. The key thing to bear in mind is that the Server and Admin API calls require the Secret Key, which must never be exposed to anyone outside your team. Otherwise, your title's security will be compromised.

0 Likes 0 ·
activedreaminc avatar image activedreaminc brendan commented ·

Hi @Brendan, thank you for your reply, there is no error message and the statistics from the new player is not set and shown. Even though it is defined in the Leaderboards such as Level, XP. I have enabled Server and Admin API via the editor extension now. Do you have example code in C# to GetPlayerStatistics and UpdatePlayerStatistics using Server or Admin API?

Thank you for your help and support

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

private void ValidateAccountInfo()

 {
 PlayFabClientAPI.GetAccountInfo(new GetAccountInfoRequest()
 {
 
 }, result =>
 {
 if (result.AccountInfo.TitleInfo.DisplayName != null)
 {
 MainMenuCanvas.SetActive(true);
 
 DisplayNameText.text = result.AccountInfo.TitleInfo.DisplayName;
 
 PhotonNetwork.playerName = DisplayNameText.text;
 
 GetPlayerInfo(result.AccountInfo.PlayFabId);
 
 
 }
 else if (result.AccountInfo.TitleInfo.DisplayName == null)
 {
 RegisterDisplayNameCanvas.SetActive(true);
 }
 
 }, error =>
 {
 Debug.LogError(error.ErrorMessage);
 });
 }
 
 private void GetPlayerInfo(string playfabID)
 {
 GetPlayerCombinedInfoRequestParams paramInfo = new GetPlayerCombinedInfoRequestParams()
 {
 GetTitleData = true,
 GetUserInventory = true,
 GetUserVirtualCurrency = true,
 GetUserAccountInfo = true,
 GetPlayerProfile = true,
 GetPlayerStatistics = true,
 GetCharacterList = true,
 GetCharacterInventories = true,
 GetUserData = true,
 GetUserReadOnlyData = true
 };
 
 PlayFabClientAPI.GetPlayerCombinedInfo(new GetPlayerCombinedInfoRequest()
 {
 PlayFabId = playfabID,
 InfoRequestParameters = paramInfo
 
 }, result =>
 {
 GetPlayerStats();
 
 }, error =>
 {
 Debug.LogError(error.ErrorMessage);
 });
 }
 
 
 private void GetPlayerStats()
 {
 PlayFabClientAPI.GetPlayerStatistics(new GetPlayerStatisticsRequest()
 {
 
 
 }, result =>
 {
 if(result.Statistics == null)
 {
 PlayFabClientAPI.UpdatePlayerStatistics(new UpdatePlayerStatisticsRequest()
 {
 Statistics = new List<StatisticUpdate>
 {
 new StatisticUpdate { StatisticName = "Level", Value = 1},
 new StatisticUpdate { StatisticName = "EXP", Value = 0},
 new StatisticUpdate { StatisticName = "Win", Value = 0},
 new StatisticUpdate { StatisticName = "Lose", Value = 0},
 }
 }, OK =>
 {
 Debug.Log("New User entered game");
 
 }, NotOK =>
 {
 Debug.LogError(NotOK.ErrorMessage);
 });
 }
 else
 {
 foreach (var eachstat in result.Statistics)
 {
 if (eachstat.StatisticName == "Level")
 {
 PlayerLevelText.text = eachstat.Value.ToString();
 }
 if (eachstat.StatisticName == "EXP")
 {
 PlayerEXPText.text = eachstat.Value.ToString();
 }
 }
 }
 
 
 }, error =>
 {
 Debug.LogError(error.ErrorMessage);
 });
 }

Hi @Brendan, thank you for your reply. The above are my codes, 
my Title ID is CA1F. The Player Ingrams is the one I already added the statistics manually, however the codes above are not working to other players such as Grudiev or any other ones that I have not added the Statistics manually. Also, how to access PlayFabServerAPI or PlayFabAdminAPI from C#?

Thank you for your help
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.