question

Kim Strasser avatar image
Kim Strasser asked

Is it possible to get the new value in the client code right after running a cloudScript?

Is it possible to get the new value of the key "Country" in the client code if I use "return server.GetUserReadOnlyData({PlayFabID: currentPlayerId, Keys: "Country"});" in my cloudScript?

Console.WriteLine(result.Result.???);

I want to get the new value of the key "Country" in Result. Is this possible?

Or is it necessary to call "var resultprofile = await PlayFabServerAPI.GetUserReadOnlyDataAsync(new PlayFab.ServerModels.GetUserDataRequest()" to get the new value of the key "Country" in the client code?

Client code:

 private async Task UpdateCountry()
 {
     var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
     {
         FunctionName = "UpdateCountryReadOnlyData",
         FunctionParameter = new { PlayerDataCountry = "Germany" },
         GeneratePlayStreamEvent = true
     });


     if (result.Error != null)
         Console.WriteLine(result.Error.Error.ToString());
     else
     {
         if (result.Result.Logs.Count() > 0)
             Console.WriteLine(result.Result.Logs[0].Message);
         else
             Console.WriteLine("Country: " + result.Result.???);
     }
 }

CloudScript:

handlers.UpdateCountryReadOnlyData = function (args, context)
{
   var NewCountry = args.PlayerDataCountry; 
   var resultdata = server.GetUserReadOnlyData({PlayFabId: currentPlayerId, Keys: "Country"}); // first search for the current country info
   var CurrentCountry = ""; 
   
   if(resultdata.Data.hasOwnProperty("Country")){ // if country info already exists in readonly data 
       
          if ((resultdata.Data.Country.Value != null) && (resultdata.Data.Country.Value != "")){ // make sure the value is not null or ""
   
                CurrentCountry = resultdata.Data.Country.Value;
                log.info("Already has country info, current country is:" + CurrentCountry.toString()); // if country value already exists, notify the client that Country info already been set
            }
          else{ // if country value is null or "", set the Country value
              server.UpdateUserReadOnlyData({
           PlayFabId: currentPlayerId,
           Data: {
               "Country": NewCountry
           }
           });
          }
   }
   else{// if country  hasn't been set yet, set it for the user
       server.UpdateUserReadOnlyData({
           PlayFabId: currentPlayerId,
           Data: {
               "Country": NewCountry
           }
           });
   }
   return server.GetUserReadOnlyData({PlayFabID: currentPlayerId, Keys: "Country"}); // return the current country info to the clients.
}
CloudScript
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

·
Citrus Yan avatar image
Citrus Yan answered

Hi Kim,

It would be cumbersome and error-prone to retrieve the country value directly from FunctionResult in ExecuteCloudScriptResult with GetUserDataResult wrapped in it.

I came up with two options for you:

1)Directly return country value from cloudscript and access it from the client. Change you CloudScript code (line 31) to “return server.GetUserReadOnlyData({PlayFabID: currentPlayerId, Keys: "Country"}).Data.Country.Value;” and use “Console.WriteLine("Country is " + result.FunctionResult.ToString());” to retrieve it from the client.

2)Don’t return the result of server.GetUserReadOnlyData in CloudScript, simple remove line 31. Use PlayFabClientAPI.GetUserReadonlyData to retrieve country info in the success callback of ExecuteCloudScriptAsync from the client in case that user data hasn’t been updated yet for some reasons. And, I think this way can make a balance between server API and client API calls.

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.