question

duartedd avatar image
duartedd asked

cloudscript and parse getplayerstats

Hello

I am trying to figure out the code for getting player stats in cloudscript.

If the Result is below:

"Statistics":[{"StatisticName":"Points","Value":570,"Version":1}

then it appears that there is no key/Value pair as in with the titledata scenario

normally for titledata i would separate the KEY/VALUE PAIRS and i can iterate through them.

I cant do this here because the structure of the result

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
  "code": 200,
  "status": "OK",
  "data": {
    "PlayFabId": "621EF2023753444",
    "Statistics": [
      {
        "StatisticName": "Points",
        "Value": 570,
        "Version": 1
      },
      {
        "StatisticName": "Wins",
        "Value": 15,
        "Version": 0
      }
    ]
  }
}
var specificSTATVALUE = JSON.parse(myResult.Statistics["Points"]);

specificSTATVALUE would = 570

Do you guys have any documentation/tutorials/examples that worked around this without having to do something like iterate through each and compare the statistic name and get the Value from that condition being met?

thank you!

10 |1200

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

JayZuo avatar image
JayZuo answered

In cloudscript, the returned Statistics is an Array. To find the specific statistic, we can use Array.find() method. For example:

var specificSTAT = myResult.Statistics.find(s => s.StatisticName === "Points");
var specificSTATVALUE = specificSTAT.Value;

And for your getPlayerStatsReq, it looks right, although I'm not sure how you get the player, and below is a simple sample which get current player's statistic value by name:

handlers.getPlayerStatisticByName = function (args, content) {
    let result = server.GetPlayerStatistics({ PlayFabId: currentPlayerId, StatisticNames: args.Name });
    log.info(result.Statistics);
    let statistic = result.Statistics[0];
    return statistic.Value;
}

In client you can use it like

PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
{
    FunctionName = "getPlayerStatisticByName",
    FunctionParameter = new { Name = "Points" }, // The parameter provided to your function
    GeneratePlayStreamEvent = true,
},
result =>
{
    Debug.Log(result.FunctionResult);
},
error =>
{
    Debug.Log("Cloud Script call failed");
    Debug.Log(error.GenerateErrorReport());
})
1 comment
10 |1200

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

duartedd avatar image duartedd commented ·

great thanks alot JayZuo - that getstatsplayer function is an argument from another function which i just pass the currentPlayerId to that getplayerstatsbyname function - thanks again!

0 Likes 0 ·
duartedd avatar image
duartedd answered

Hm So - I guess iterating through only 1 is not so bad if i just request the one specific StatNAME

but that brings up this - i was looking for the proper syntax for the request within cloudscript but the JS button (nor any of the other ) doesnt work for me - anyone else have this issue? Just defaults to unity

or anyoen know the proper syntax for cloudscript? eg

eg

  var getPlayerStatsReq = 
    {
        PlayFabId: player,
        StatisticNames: [ "Points" ] };

https://api.playfab.com/docs/tutorials/landing-players/player-statistics

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.