question

justas.antanauskas@gmail.com avatar image
justas.antanauskas@gmail.com asked

CloudScript return bug. FunctionResult is null

I was updating my game code to a new PlayFab version. Mainly replacing RunCloudScript with ExecuteCloudScript and encountered bizzare bug with return values.
Example cloudscript code:

handlers.TestReturn1 = function (args)
{
var message = "Hello " + currentPlayerId + "!";
return { messageValue: message };
}

It works alright. But when i do this -

handlers.TestReturn1 = function (args)
{
var message = "Hello " + currentPlayerId + "!";
return
{
messageValue: message
};
}

Suddenly FunctionResult returns null. What the hell? I have been sitting here for hours trying to figure out why my functions returning null. Thinking it was my fault with how i'm handling Arrays, but no.. its this minuscule thing.
I had replicated it few times.

10 |1200

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

brendan avatar image
brendan answered

That's actually not a bug - it's a "feature" of JavaScript called Automatic Semicolon Insertion (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion). So you can say:

return { messageValue: message };

or even

return {
  messageValue: message
};

but you can't have a return line without the expression at least starting on the same line, as the semicolon will automatically be added, causing you to return nothing.

10 |1200

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

justas.antanauskas@gmail.com avatar image
justas.antanauskas@gmail.com answered

Ah. Thanks for clarifying. I somewhat expected it could be JavaScript thing, since i have little experience with it. It is confusing feature for someone who mainly uses 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.

brendan avatar image
brendan answered

Agreed - and that's not the only thing that's a bit odd in JavaScript.  :)

10 |1200

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

james-1 avatar image
james-1 answered

I'm running into a similar issue with my cloudscript returning null for FunctionResult. I used the ProgressiveRewards recipe as a starting point and have mainly just changed the reward lookup to use EvaluateRandomResultTable. The rewards are properly granted to the user but I am not able to do anything with the FunctionResult on the client side.

On player login, the client calls cloudscript "CheckIn":

handlers.CheckIn = function(args) {

// removed additional logic for brevity

var grantedItems = [];


var EvaluateRandomResultTableRequest = {
    "TableId" : DAILY_SPIN_REWARD_TABLE,
    "CatalogVersion" : DAILY_SPIN_CATALOG
};


var EvaluateRandomResultTableResult = server.EvaluateRandomResultTable(EvaluateRandomResultTableRequest);


	if(EvaluateRandomResultTableResult["ResultItemId"])
	{
		grantedItems = GrantItems(EvaluateRandomResultTableResult["ResultItemId"], tracker[TRACKER_LOGIN_STREAK]);
	}
	
	return grantedItems;
}
return [];
};

Which calls GrantItems:

function GrantItems(items, count)
{
log.info("Granting: " + items);
if(count > 7)
count = 7;

    var parsed = Array.isArray(items) ? items : [ items ];
    if(Array.isArray(items))
    {
	parsed = items;
    }
    else
    {
	parsed = new Array(count);
	for(var i = 0; i < count; i++)
	    parsed[i] = items;
    }
    var GrantItemsToUserRequest = {
    	"CatalogVersion" : DAILY_SPIN_CATALOG,
        "PlayFabId" : currentPlayerId,
        "ItemIds" : parsed,
        "Annotation" : "Granted for logging in over " + count + " consecutive days."
    };

    var GrantItemsToUserResult = server.GrantItemsToUser(GrantItemsToUserRequest);

    return GrantItemsToUserResult.ItemGrantResults;
}

Anything obvious that I'm doing wrong to get the correct FunctionResult?

Thanks,

James

10 |1200

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

brendan avatar image
brendan answered

When I test using this code in my own Title ID, I'm seeing the FunctionResult no problem. What do you see when you run this in the Game Manager on a Player? What Title ID is this you're testing?

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.