question

hendra avatar image
hendra asked

Get Return Value From Cloud Script

Hi, how do i read the return value from my cloud script?

So here's my cloud script code

var result = [];
    var getLeaderboardResult = server.GetLeaderboard
        (
        {
                "StatisticName": "PLAYER_SCORE",
                "MaxResultsCount": args.amount,
                "StartPosition": 0
        }
    );
    var getCharLeaderboardResult = server.GetUserReadOnlyData
        (
        {
                "PlayFabId": currentPlayerId,
                "Keys": ["LEADERBOARD_CHARACTER"]
        }
    );
    result.push(getLeaderboardResult);
    result.push(getCharLeaderboardResult);
    return result;

On my client (Unity), in the executeCloudCode success callback, what should i do so i can get the leaderboard value (score, name, etc) and the readOnly data?

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

·
Joshua Strunk avatar image
Joshua Strunk answered

Just presenting the easiest and most generic solution though possibly not the most performant

In C#

1) Create an object in C# which maps to the returned object from CloudScript

2) var jsonEncodedResult = result.FunctionResult.ToString()

3) var resultObject = JsonLibrary.DeserializeObject<MappingObject>(jsonEncodedResult)

In Cloud Script

Return normal JS objects which will line up with your C# type

So in your case stop returning an array and instead just have result be a normal JS object.

var result = { };

Then instead of result.push() use result.leaderboardResult = getLeaderboardResult, etc... and on client you would have some

public class CloudscriptRequestObject {

   public GetLeaderBoardResult leaderboardResult;

   public GetUserDataResult charLeaderboardResult;

}

Some other relevant discussions on this topic

https://community.playfab.com/questions/4521/executecloudscript-functionresult-frustration.html

https://community.playfab.com/questions/12060/cloudscript-return-issue.html

https://community.playfab.com/questions/5276/what-object-type-does-cloudscript-return-in-its-fu.html


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.

hendra avatar image hendra commented ·

Thanks for the answer :D

Few more questions though

1. So i should use JsonLibrary not JsonWrapper?
2. So in my case i have "GetLeaderboardResult" and "GetUserDataResult", and return it in an object format. In c#, to get each of the result, i can do this?


var jsonEncodedResult = result.FunctionResult.ToString();
var leaderboardResult = JsonLibrary.DeserializeObject<GetLeaderboardResult>(jsonEncodedResult);
var userDataResult = JsonLibrary.DeserializeObject<GetUserDataResult>(jsonEncodedResult);
1 Like 1 ·
Joshua Strunk avatar image Joshua Strunk hendra commented ·

1. JsonLibrary is just a placeholder for any Json library/framework you want to use.

2. You cannot deserialize the result object into 2 differently typed objects. You will need to create your own custom type which can act as a container for both the leaderboardResult and userDataResult.

2 Likes 2 ·

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.