question

helloza123 avatar image
helloza123 asked

How to send json string as a parameter to execute cloud script?

Hello, I try sending json data as function parameter to save player data in bulk. However, after I send request to save data and the call is successful, nothing is saved at all. I tried it in Game Manager and it work fine but doesn't work when run on Unity. Here is my client code

public void SavePlayerData(){

        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
        {
            FunctionName = "SavePlayerData",
            FunctionParameter = new {dataToSave = playerData.DataToJson ()},
            GeneratePlayStreamEvent = true
        }, result =>
        {
            string log = result.Logs[0].Message;

            print(log);
            print("save data complete");
        },
        OnRequestFailure);
}

public string DataToJson()
{
        JObject jsonData = new JObject();
        jsonData["name"] = "myName";

        return jsonData.ToString();
}

Here is my server code

handlers.SavePlayerData = function(args, context)
{
    var dataToSave = args.dataToSave; //as JSON
    log.debug(dataToSave);
    var request = server.UpdateUserData({PlayFabId: currentPlayerId, Data: dataToSave});
}

10 |1200

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

Rick Chen avatar image
Rick Chen answered

The “Data” property in UpdateUserData server API should be a object type, not json. Therefore in your client code, you don’t need to convert the data to json. You can replace line 6 of your client code with

FunctionParameter = new { dataToSave = new { name= "myName" } },

Or if you want to save the json data as a value (not key/value pair) in Player data, you can change your CloudScript code to:

handlers.SavePlayerData = function(args, context)
{
    var dataToSave = JSON.stringify(args.dataToSave); //as JSON
    log.debug(dataToSave);
    var request = server.UpdateUserData({PlayFabId: currentPlayerId, Data: {myData:dataToSave}});
}


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.

helloza123 avatar image helloza123 commented ·

It's working now. Thank you for the solution.

0 Likes 0 ·
Rick Chen avatar image
Rick Chen answered

I will do some tests about this issue. Meanwhile, could you please replace your line 6 with

 FunctionParameter = new { dataToSave = "{\"name\":\"myName\"}" },

and try again?

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.

helloza123 avatar image helloza123 commented ·

I've tried it but still nothing is saved to player data.

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.