Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by patrickdib007 · Mar 06, 2021 at 06:47 PM · CloudScriptTitle DataCharacter DataCharactersContent

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

Comment

People who like this

0 Show 2
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ivan Cai ♦ · Mar 08, 2021 at 07:44 AM 0
Share

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

avatar image patrickdib007 Ivan Cai ♦ · Mar 08, 2021 at 12:05 PM 0
Share
{

"Character1":{


"HP":"4500",

"ATK":"550",

"DEF":"200",

"CRIT":"1.75",

"CHANCE":"20"}

}

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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 ?

Comment

People who like this

0 Show 1 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ivan Cai ♦ · Mar 18, 2021 at 06:24 AM 0
Share

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

avatar image

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.

Comment

People who like this

0 Show 5 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image patrickdib007 · Mar 10, 2021 at 12:08 AM 0
Share
{
    "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"])

avatar image Ivan Cai ♦ patrickdib007 · Mar 10, 2021 at 07:48 AM 0
Share

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.

avatar image patrickdib007 Ivan Cai ♦ · Mar 10, 2021 at 11:55 PM 0
Share

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)"
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    3 People are following this question.

    avatar image avatar image avatar image

    Related Questions

    Character Creation guide. 1 Answer

    Get Character ID from the Server 1 Answer

    Is it possible to use the SetObjects function from Cloudscript? 1 Answer

    Cloudscript update player data only for not existing keys 0 Answers

    Updating Character Inventory from Server 1 Answer

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges