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
Can you provide the structure of data(Key=Characters) that stored in your title data?
{ "Character1":{ "HP":"4500", "ATK":"550", "DEF":"200", "CRIT":"1.75", "CHANCE":"20"} }
Answer by patrickdib007 · Mar 12, 2021 at 12:03 AM
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 ?
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
Answer by Ivan Cai · Mar 09, 2021 at 06:18 AM
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.
{ "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"])
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.
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)"