question

Robert avatar image
Robert asked

Update Character Data

So I'm trying to update character data after a player creates a character, but because it's server api I didn't wanna run it off the client since it's not secure. I wanted the cloudscript to set the new character's level upon creation and using the following code;

handlers.SetupNewChar = function(args, context)
{
  var dat = {};
  dat["Level"] = "1";
  var request = new UpdateCharacterDataRequest()
  {
    PlayFabId = currentPlayerId,
   	CharacterId = args.CHID,
    Data = dat
  };
  var err = new PlayFabError();
  var res = new UpdateCharacterDataResult();
  server.GrantCharacterToUser(request, res, err);                              
  
  log.info("Creating Character: " + args.CHID + " for " + currentPlayerId);
};

But every-time it runs it fails and I'm not sure what it is that's incorrect. At first I wasn't passing the details correctly and I realized I had to put a .5 second delay via yield in my code to await a response, not sure if I'm supposed to do it that way, but it's the only way I could get 2 requests to work back-to-back. Even after I see the char ID this is still failing, is it a syntax thing?

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

Robert avatar image Robert commented ·

Just realized I copied an old revision where I'd used GrantCharacterToUser by accident, my live version does have the correct UpdateCharacterData function.

0 Likes 0 ·
brendan avatar image
brendan answered

Ah! Sorry, should have spotted it above. In your call to UpdateCharacterData in the Cloud Script, you have:

  Data:[{
    "Level":"1"
  }]

The data is an object - specifically, a dictionary. So that should be:

  Data:{
    "Level":"1"
  }

Or you could use the code where you defined your data like so:

  var dat = {};
  dat["Level"] = "1";

And then use:

  Data:dat
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.

Robert avatar image Robert commented ·

Thank you very much, what I had done was copied the template from the help page Statistics: [{ "StatisticName": "xp", "Value": 10 }] without removing the blasted [ ]s. That was exactly the problem.

0 Likes 0 ·
brendan avatar image
brendan answered

What's the result you're getting back from the call? Can you try putting it in a try/catch to check any errors, and logging the response? By the way, the (request, response, error) pattern is not valid in the context of a Cloud Script - that's the syntax used by our SDKs. For Cloud Script, you would only pass in the request, and get the response the normal way (var res = server.xxx).

10 |1200

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

Robert avatar image
Robert answered

Well I modified the code late last night to:

var updateCharData = server.UpdateCharacterData({
    
    PlayFabId : currentPlayerId,
    CharacterId : args.CHID,
    Data : [{
      "Level" : "1"
    }]    
  });

...because as I was thinking it over I realized my syntax was off, Idk why my brain wanted to use JS instead of json. Now though, the playstream data is telling me (SUCCESS: SetupNewChar()
) it's run successfully, but when I look at the character data, it's not been modified.

So I decided to look into my code and use debug info from the cloudscript result.

var req = new ExecuteCloudScriptRequest()
        {
            FunctionName = "SetupNewChar",
            FunctionParameter = new
            {
                CHID = charID                           
            },
            GeneratePlayStreamEvent = true
        };

So I put

Debug.Log(string.Format("{0} {1} {2} {3} {4}", res.ExecutionTimeSeconds, res.FunctionResult, charID, res.Revision, res.Logs[0].Message));

inside the ExecuteCloudScript result, and I get a response back with the exact right info.

Which in this particular case gave me a time of .244s and the correct charID from the log.info back. So, I'm back to wondering what's wrong.

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.

brendan avatar image brendan commented ·

What's the Title ID for this? I'd like to be able to review the whole script, so that I can understand the complete flow.

0 Likes 0 ·
Robert avatar image Robert brendan commented ·

Titleid is EB6C. Revision 11 is literally just this one function.

Complete code in client for the request is:

var req = new ExecuteCloudScriptRequest()
        {
            FunctionName = "SetupNewChar",
            FunctionParameter = new
            {
                CHID = charID                           
            },
            GeneratePlayStreamEvent = true
        };
        //Debug.Log(charID);
        PlayFabClientAPI.ExecuteCloudScript(req, (ExecuteCloudScriptResult res) =>
        {
            Debug.Log(string.Format("{0} {1} {2} {3} {4}", res.ExecutionTimeSeconds, res.FunctionResult, charID, res.Revision, res.Logs[0].Message));
        },
        (PlayFabError err) =>
        {
            Debug.LogError(err.ErrorMessage);
        });
<br>
0 Likes 0 ·

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.