I'm pulling my hair over this issue. I'm trying out Unity with PlayFab for the first time and can't even get the simplest "hello world" type cloud script call to work. Whatever I do the response comes back without FunctionResult in it. I do however get all the other data like ExecutionTimeSeconds, Revision and whatnot.
Calling the cloudscript handler/function from game manager works. Then FunctionResult is included in the response but from within Unity it isn't. I've also tried both returning a naked string as well as wrap it in a json. Same outcome.
I can see here in the forums that people have had similar issues in the past but that was ages ago and I've also made sure I'm not making any of the mistakes pointed out in those old threads.
Can you please advise? My title id is 1D80.
Thanks
PlayFabCloudScriptAPI.ExecuteEntityCloudScript(new ExecuteEntityCloudScriptRequest() { Entity = new PlayFab.CloudScriptModels.EntityKey { Id = entityId, Type = entityType }, FunctionName = "Hello", RevisionSelection = CloudScriptRevisionOption.Latest }, OnPlayFabEntityCloudScriptExecuted, OnPlayFabError/*, customData, extraHeaders*/);
handlers.Hello = function(args, context) { log.debug("Hello called!"); var response = {"msg": "OH CMON!"}; return response; }
Answer by oskar · Nov 11, 2019 at 09:53 AM
There is this caveat that if using the standard C# JsonUtility the FunctionResult won't be recognized. You have to use PlayFabs PlayFabSimpleJson to be able to log the result.
Debug.Log(JsonUtility.ToJson(result.FunctionResult)); // =>> {}
Debug.Log(PlayFabSimpleJson.SerializeObject(result.FunctionResult)); // =>> prints the response object properly
Answer by Sarah Zhang · Nov 11, 2019 at 07:03 AM
Your CloudScript function has run successfully, and you have got the function result. You can navigate to [GameManager]->[Data]->[Event History], use the [Show JSON] button (arrow icon on the item’s left) to expand the details of items [player_executed_cloudscript SUCCESS: Hello() (revision xx)], you will find the function result in their JSON. So, API and your process work fine. It looks like the problem is in the Unity’s client code, you can try to refer to the doc Writing custom CloudScript to modify it.
In addition, you can use ExecuteCloudScript instead of ExecuteEntityCloudScript in your code. ExecuteEntityCloudScript should be used when you want to execute Entity API on Cloud Script. Executing “Hello” function doesn’t need to use it.
I realize I wouldn't need to use ExecuteEntityCloudScript for this simple cloudscript function but my aim is to eventually make use of Entity Api so this was just a first little step where I unfortunately ran into problems right away.
You're right tho that the issue was in my client code. By using the code at the bottom I can get a hold of the returned value. Very frustrating tho that when just logging the full result of the cloudscript function with
Debug.Log(JsonUtility.ToJson(result.FunctionResult)); // =>> {}
the sdk treats it as if the FunctionResult just isn't there. Very un-intuitive.
private static void OnCloudHello(ExecuteCloudScriptResult result) { Debug.Log(PlayFabSimpleJson.SerializeObject(result.FunctionResult)); JsonObject jsonResult = (JsonObject)result.FunctionResult; object responseMessage; jsonResult.TryGetValue("msg", out responseMessage); Debug.Log((string)responseMessage); }