question

Richard Barker avatar image
Richard Barker asked

I am having trouble calling two linked functions in Cloudscript. Possibly due to to json, parse json errors.

I am trying to get a leaderboard from Cloudscript and then for each entry get userdata. I think my issues are with json but the result when I run from the client is "JSON must represent an object Type". BTW I am aware of limits on number of requests per Cloudscript call, this would be for a small excerpt of the leaderboard. Can anyone help please!

Here is the client code.

        PlayFabClientAPI.ExecuteCloudScript(
            new ExecuteCloudScriptRequest()
            {
                FunctionName = "getLeaderboard", 
                FunctionParameter = new
                {
                    leaderboardName = leaderboardName,
                    maxResults = MaxRequests,
                    start = start
                },
            }, 
            result =>
            {
                List<PlayerData> list = JsonUtility.FromJson<List<PlayerData>>(result.FunctionResult.ToString());
                Debug.Log(list);
            },
            error =>
            {
                Debug.Log(error);
            }
        );

Here is the cloudscript code

// Get leaderboard and return list of players
handlers.getLeaderboard = function (args, context) {


    var leaderboardName= args.leaderboardName;
    var maxResults = args.maxResults;
    var start = args.start;


    var request = {
		StatisticName: leaderboardName,
		StartPosition: start,
		MaxResultsCount: maxResults
    };
    var response = server.GetLeaderboard(request);
	var leaderBoard = response.Leaderboard;


	var playerList = [];
	for (var i=0; i<leaderBoard.length; i++)
	{
		var entry = leaderBoard[i];
		var request = {
			PlayFabId: entry.PlayFabId
		};
		var response = server.GetUserData(request);
		var playerData = response.Data["Player"].Value;
		var myPlayerObject = JSON.parse(playerData);
		playerList.push(myPlayerObject);
	}
	
	return playerList;


};
10 |1200

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

Richard Barker avatar image
Richard Barker answered

Thanks for your reply! You put me on the right track. I removed the parsing code from Cloudscript and processed it on the client.

The real problem was I expected to parse the list returned as json in an easy List<PlayerData> type way. Actually I has to do this to make it work (it could perhaps be simplified).

                List<PlayerData> newList = new List<PlayerData>();
                object jsonObject = result.FunctionResult;
                List<object> list = (List<object>) jsonObject;
                foreach(object listItem in list)
                {
                    PlayerData pd = JsonUtility.FromJson<PlayerData>(listItem.ToString());
                    newList.Add(pd);
                }


10 |1200

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

Seth Du avatar image
Seth Du answered

I have tested your codes and the API usage is fine. The issue can be the JSON usage. I am not sure how you defined the List<PlayerData>, does it contain any other data (like player ID) besides the values of “player data”?

Anyway, you may consider remove JSON.parse() on line 28 of your cloud script part and move the complete parsing JSON process to client side. Moreover, if you can provide your title ID, I can fully test it because I need to know how you define Player Data "Player".

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.