question

Kim Strasser avatar image
Kim Strasser asked

Is it possible to use the cloud script function result as a dictionary?

What is the easiest way to get the cloud script function result keys/values in the client so that I can use the values in the client code? Is it possible to use the cloud script function result as a dictionary?

Or is it necessary that I split the long string(result.Result.FunctionResult) somehow into pieces so that I can use the values in the client? I want to get this result in the client:

AddedNewValue = true;
NewValue = 2500;
ScoreRating = "Good";

For example, I have this cloud script and client code:

handlers.UpdateLeaderboard = function (args, context)
{
    // ...
    var newvalue = 2500;
    var addednewvalue = true;
    var rating = "Good";
    var resultdata = "Added value:" + addednewvalue.toString() + " " + "New value:" + newvalue.toString() + " " + "Rating:" + rating;
    return resultdata;
}
bool AddedNewValue = false;
int NewValue;
string ScoreRating;
private async Task UpdatePlayerHighscore(string leaderboardname, string playerscore, string level)
{
    var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
    {
        FunctionName = "UpdateLeaderboard",
        FunctionParameter = new { Leaderboardname = leaderboardname, Playerscore = playerscore, Level = level },
        GeneratePlayStreamEvent = true
    });

    if (result.Error != null)
        Console.WriteLine(result.Error.Error.ToString());
    else
    {
        if (result.Result.FunctionResult != null)
        {
            AddedNewValue = ...?
            NewValue = ...?
            ScoreRating = ...?
        }
    }
}
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

·
Sarah Zhang avatar image
Sarah Zhang answered

Yes, it’s possible to convert the cloud script function result into a dictionary without splitting the long string. Firstly, please let CloudScript return a function result that has a JSON string format. Secondly, please use PlayFabSimpleJson.DeserializeObject in clients‘ code to convert a JSON string to a dictionary. You can refer to the following code, and modify it for your needs.

Cloud script function example:

handlers.UpdateLeaderboard = function (args, context) {
    var resultdata = {
        "Added value": true,
        "New value": 2500,
        "Rating": "Good"
    };
    return resultdata;
};

The client code example:

…
using PlayFab.Json;
…
public bool addedValue;
public int newValue;
public string rating;
…
if (result.Result.FunctionResult != null)
{
        Dictionary<string,object > dic = PlayFabSimpleJson.DeserializeObject<Dictionary<string, object>>(result.FunctionResult.ToString());
        if (dic.TryGetValue("Added value", out object a))
        {
           addedValue = Convert.ToBoolean(a);
        }
        if (dic.TryGetValue("New value", out object b))
        {
            newValue = Convert.ToInt32(b);
        }
        if (dic.TryGetValue("Rating", out object c))
        {
            rating = Convert.ToString(c);
        }

}
…

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.