question

Travis Lange avatar image
Travis Lange asked

Cloud Script FunctionResult Returns Null

We are currently following the PlayFab tutorial Writing Custom Cloud Script. The problem is, the FunctionResult is always null in the Unity SDK. The log and everything is populated correctly, and the call is successful, but the FunctionResult is null. Am I doing something incorrectly or is this a bug?

// Cloud Script
handlers.helloWorld = function (args, context) {
    var message = "Hello " + currentPlayerId + "!";
    log.info(message);
    var inputValue = null;
    if (args && args.hasOwnProperty("inputValue"))
        inputValue = args.inputValue;
    log.debug("helloWorld:", { input: inputValue });
    return { messageValue: message };
}

// C# Unity SDK
private static void OnCloudHelloWorld(ExecuteCloudScriptResult result) {
    // Cloud Script returns arbitrary results, so you have to evaluate them one step and one parameter at a time
    Debug.Log(JsonWrapper.SerializeObject(result.FunctionResult));
    JsonObject jsonResult = (JsonObject)result.FunctionResult;
    object messageValue;
    jsonResult.TryGetValue("messageValue", out messageValue); // note how "messageValue" directly corresponds to the JSON values set in Cloud Script
    Debug.Log((string)messageValue);
}
unity3dsdksCloudScript
10 |1200

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

Travis Lange avatar image
Travis Lange answered

I solved my issue! :) It turns out you have to make sure your request matches the method signature of the JavaScript function (including parameters a.k.a 'args').

Example:

As can be seen, the CloudScript function 'handlers.helloWorld' is looking for a parameter 'args.' If that parameter isn't initialized to something in C#, that function won't get hit. That is why initializing the 'FunctionParameter' in example '2.)' to anything besides 'null' works. In example '1.)', the 'FunctionParameter' is null meaning it is looking for a method that doesn't take any parameters.

(I haven't tested out 'context' yet, but as far as I can tell it seems to ignore that as part of the method signature).

// CloudScript

handlers.helloWorld = function (args, context) {
    var message = "Hello " + currentPlayerId + "!";
    log.info(message);
    var inputValue = null;
    if (args && args.hasOwnProperty("inputValue"))
        inputValue = args.inputValue;
    log.debug("helloWorld:", { input: inputValue });
    return { messageValue: message };
}
// 1.) C# Doesn't Work

var request = new ExecuteCloudScriptRequest{ FunctionName = "helloWorld", GeneratePlayStreamEvent = true };
PlayFabClientAPI.ExecuteCloudScript(request, OnExecuteCouldScriptRequstSuccess, OnPlayFabError);
// 2.) C# Works

var request = new ExecuteCloudScriptRequest{ FunctionName = "helloWorld", FunctionParameter = false, GeneratePlayStreamEvent = true }; 
PlayFabClientAPI.ExecuteCloudScript(request, OnExecuteCouldScriptRequstSuccess, OnPlayFabError);
10 |1200

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

Andy avatar image
Andy answered

An empty FunctionResult when you expected to get something back from the Cloud Script usually means the script failed before completion. The Error property would contain more information. I don't see an obvious error above, but I'm not a Javacript interpreter. :)

After the fact, you can get more information about the failure by ensuring you call cloud script with GeneratePlayStreamEvent set to true. You can then look at the Event history to debug.

1 comment
10 |1200

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

Travis Lange avatar image Travis Lange commented ·

We ended up solving our issue in the answer above, but that suggestion for the Event history has helped us a ton when it comes to debugging!

0 Likes 0 ·

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.