question

gooby avatar image
gooby asked

Cloud script ExecuteFunction parameters not working

I've been trying to use cloud functions in playfab and for some reason it doesn't recognize the parameters I pass from unity's sdk. I tried both azure and digitalOcean cloud functions. for the FunctionParameter field, ive tried dictionaries, objects, etc. There is very little info online that isnt outdated, so i was hoping cloud functions still work?

Ive attached the playfab function above (with a valid URL, just whited out)

Here is the azure function code (basically default):

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    string responseMessage = string.IsNullOrEmpty(name)
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
        : $"Hello, {name}. This HTTP triggered function executed successfully.";

    return new OkObjectResult(responseMessage);
}

And here is my unity call:

            PlayFabCloudScriptAPI.ExecuteFunction(
                new ExecuteFunctionRequest
                {
                    FunctionName = "AzureTest",
                    GeneratePlayStreamEvent=true,
                    FunctionParameter = new
                    {
                        name = "chicken"
                    }
                },
                result =>
                {
                    Debug.Log(result.ToJson());
                },
                err =>
                {
                    Debug.LogError(err.GenerateErrorReport());
                }
            );

The call is successful, but always returns this as if i sent no parameters:

{"Error":null,"ExecutionTimeMilliseconds":627,"FunctionName":"AzureTest","FunctionResult":"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.","FunctionResultTooLarge":null,"Request":{"CustomTags":null,"Entity":null,"FunctionName":"AzureTest","FunctionParameter":{"name":"pinga"},"GeneratePlayStreamEvent":true,"AuthenticationContext":null},"CustomData":null}
UnityEngine.Debug:Log (object)

Ive just started learning cloud functions in playfab so if there is a good way to debug this stuff, Id appreciate info about that as well

sdksCloudScript
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

·
JayZuo avatar image
JayZuo answered

The problem here is that while calling ExecuteFunction, PlayFab will not pass the FunctionParameter to your Azure function as a normal HTTP request you've seen in Azure Function guide. Instead, PlayFab will set a context which includes the following information in the request body:

  • The Entity Profile of the caller
  • The Title Authentication Context
  • A boolean that indicates whether a Playsteam event is sent as part of the function being executed
  • The functions arguments used when calling the script

More details can be found at https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context#use-the-context-model-when-executing-via-the-executefunction-api.

So, in your Azure function, "data?.name;" won't get the FunctionParameter. And when you Register Function, I'd believe you are setting the Function URL like

https://<functionappname>.azurewebsites.net/api/AzureTest

As there is also no "name" query string in the URL,

string name = req.Query["name"];

will also be null. This is why your function always returns this as if you sent no parameters.

To get the name you've set in FunctionParameter with ExecuteFunction method, you will need to change your Azure function's code to

name = data?.FunctionArgument?.name;

Besides, we'd suggest you can check out the Azure Function Cloud Script Example code in PlayFab CloudScript using Azure Functions Quickstart Guide, which used "FunctionExecutionContext" in CS2AFHelperClasses.cs to get the function parameters.

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.