question

patrickdib007 avatar image
patrickdib007 asked

Updating Character Statistics Cloudscript

Hi, I have a little trouble updating my characters statistics for setting up a character. Here is my code:

handlers.CreateCharacter = function (args, context)
{
    var name = args.CharacterName;
   
    var characters = server.GetTitleData({ Keys: "Characters"});


    var Data = characters.Data["Characters"];


    var parsedData = JSON.parse(Data);


    var request = {
        PlayFabId: currentPlayerId,
        CharacterName: name,
        CharacterType: "Swordsmen"
    };


   var characterid =  server.GrantCharacterToUser(request);


   var UpdateCharacterStatisticsRequest = {
        PlayFabId: currentPlayerId,
        CharacterId: characterid,
        CharacterStatistics: parsedData[name] // I know that this is wrong
    };


    log.info(UpdateCharacterStatisticsRequest);



//Cant seem to do this part successfully:


var UpdatePlayerStatisticsResult = server.UpdateCharacterStatistics(UpdateCharacterStatisticsRequest);
};

I can't figure out how to but each value into the statistics since CharacterStatistics is defined as an object.

Thanks for your help

CloudScriptTitle DataCharacter DataCharactersContent
2 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.

Ivan Cai avatar image Ivan Cai ♦ commented ·

Can you provide the structure of data(Key=Characters) that stored in your title data?

0 Likes 0 ·
patrickdib007 avatar image patrickdib007 Ivan Cai ♦ commented ·
{

"Character1":{


"HP":"4500",

"ATK":"550",

"DEF":"200",

"CRIT":"1.75",

"CHANCE":"20"}

}
0 Likes 0 ·
patrickdib007 avatar image
patrickdib007 answered

I found out why it didn't work. The time between server.GrantCharacterToUser and the update function was too short.

Tried doing them separately and it worked fine.

So now how do I make a "Wait until GrantCharacter is finished" before doing the update characterStats function ? I know that in C# it would be an IEnumerable, but in JS maybe there is another way ?

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.

Ivan Cai avatar image Ivan Cai ♦ commented ·

You can do it in cloudscript. For more operations,you can refer to https://community.playfab.com/questions/36691/is-it-possible-to-wait-a-few-milliseconds-or-secon.html

0 Likes 0 ·
Ivan Cai avatar image
Ivan Cai answered

According to the information you provide, modify your code as follows:

handlers.CreateCharacter = function (args, context)


{


    var name = args.CharacterName;

    var characters = server.GetTitleData({Keys: "Characters"});

    var Data =characters.Data["Characters"];

    var parsedData =JSON.parse(Data);

    var characterData=JSON.parse(parsedData["Character1"])

    var request = {


        PlayFabId:currentPlayerId,


        CharacterName: name,


        CharacterType:"Swordsmen"


    };

   var characterid = server.GrantCharacterToUser(request);

   var UpdateCharacterStatisticsRequest = {


        PlayFabId:currentPlayerId,


        CharacterId:characterid,


        CharacterStatistics:characterData 


    };



    log.info(UpdateCharacterStatisticsRequest);


var UpdatePlayerStatisticsResult =server.UpdateCharacterStatistics(UpdateCharacterStatisticsRequest);


};

Please note that the server calls UpdateCharacterStatistics, there is an error that the value '1.75' is not valid for CRIT.Because statistic value can't be a floating point number.

5 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.

patrickdib007 avatar image patrickdib007 commented ·
{
    "FunctionResult": null,
    "Logs": [],
    "ExecutionTimeSeconds": 0.0437239,
    "MemoryConsumedBytes": 8944,
    "APIRequestsIssued": 1,
    "HttpRequestsIssued": 0,
    "Error": {
        "Error": "JavascriptException",
        "Message": "JavascriptException",
        "StackTrace": "SyntaxError: Unexpected token o in JSON at position 1\n    at JSON.parse (<anonymous>)\n    at handlers.CreateCharacter (4D3AB-main.js:91:30)\n    at Object.invokeFunction (Script:116:33)"
    }
}

line 91 is : var characterData=JSON.parse(parsedData["Character1"])

0 Likes 0 ·
Ivan Cai avatar image Ivan Cai ♦ patrickdib007 commented ·

I'm so sorry. When I simulated settitledata, I saved it in json string format. I apologize to you again for my mistake. After my repeated tests, there is no problem with your code. The final issue is that there is an error that the value '1.75' is not valid for CRIT. Because statistic value can't be a floating point number.

0 Likes 0 ·
patrickdib007 avatar image patrickdib007 Ivan Cai ♦ commented ·

It still gives me an error. I don't understand how to read the error:

"Error": {
        "Error": "CloudScriptAPIRequestError",
        "Message": "The script called a PlayFab API, which returned an error. See the Error logs for details.",
        "StackTrace": "Error\n    at Object.server_request (Script:180:24)\n    at Object.server.UpdateCharacterStatistics (Script:619:82)\n    at handlers.CreateCharacter (4D3AB-main.js:114:48)\n    at Object.invokeFunction (Script:116:33)"
0 Likes 0 ·
Show more comments

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.