question

Leyla Ahmedzadeh avatar image
Leyla Ahmedzadeh asked

How to update user data from C# cloud script.

I am using this script to update user data but it doesn't update. I think the problem is in the 20th line

[FunctionName("SetPlayerData")]
        public static async Task<int> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post",Route = null)] HttpRequest req, ILogger log)
        {
             log.LogInformation("C# HTTP trigger function processed a request.");
             FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
             var args = context.FunctionArgument;

             string keyname = args["name"];
             int dataValue = args["value"];

             Dictionary<string, string> data = new Dictionary<string, string>();
             data.Add(keyname,dataValue.ToString());

            var request = new PlayFab.ServerModels.UpdateUserDataRequest
            {
                 PlayFabId = args["playfabId"],
                 Data = data,
            };

            Task<PlayFabResult<UpdateUserDataResult>> task = PlayFabServerAPI.UpdateUserDataAsync(request);

             return dataValue;
        }
Player DataCloudScript
10 |1200

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

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

To use PlayFab Server API, you need to set the TitleId and DeveloperSecretKey first. Here is an example for your reference. For more info, please refer to Quickstart: Writing a PlayFab CloudScript using Azure Functions.

[FunctionName("HttpTrigger1")]
public static async Task<PlayFabResult<UpdateUserDataResult>> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
    var args = context.FunctionArgument;
    string key = args["Key"];
    string id = args["PlayFabId"];
    int value = args["Value"];
    var apiSettings = new PlayFabApiSettings{
        TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID",EnvironmentVariableTarget.Process),
        DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY",EnvironmentVariableTarget.Process)
    };
    var serverApi = new PlayFabServerInstanceAPI(apiSettings);
    var request = new UpdateUserDataRequest{
        PlayFabId = id,
        Data = new Dictionary<string,string>(){
            {
                key,value.ToString()
            }
        }
    };
    var ret = await serverApi.UpdateUserDataAsync(request);
    return ret;
}
10 |1200

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

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.