question

Murtadha avatar image
Murtadha asked

pass parameters from unity to azure function

Hello, I'm trying to get a string type from unity to pass it in my azure function. I'm using this code to provide the player an option to delete their master player account

     public class FunctionExecutionContext<T>
     {
         public PlayFab.ProfilesModels.EntityProfileBody CallerEntityProfile { get; set; }
         public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
         public bool? GeneratePlayStreamEvent { get; set; }
         public T FunctionArgument { get; set; }
     }
    
 public static class HttpExample 
     {
    
         [FunctionName("HttpExample")]
    
             public static async Task<dynamic> Run(
             [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
             ILogger log)
             {
    
     PlayFabSettings.staticSettings.TitleId = "...";
     PlayFabSettings.staticSettings.DeveloperSecretKey = "...";
     FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
                                                                                                                                            
     dynamic args = context.FunctionArgument;
    
     var task = await PlayFabAdminAPI.DeleteMasterPlayerAccountAsync(new DeleteMasterPlayerAccountRequest()
     {
         PlayFabId =  args.loggedInPlayfabId
     });
    
              string responseMessage = "success";
                
              return new OkObjectResult(responseMessage);
             }
             }
             }

I'm having a problem getting the parameter from unity to azure function. The code works fine but whenever I put this line: dynamic args = context.FunctionArgument; I get this error: Exception while executing function: HttpExample. Azure Function: Object reference not set to an instance of an object

maybe the code is correct but I need to do something in the azure portal? I'm fairly new to coding and azure function. any help would be appreaicated :)

Player Data
8 comments
10 |1200

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

Simon Cui avatar image Simon Cui commented ·

Hi, could you also provide your codes that make the request to call ExecuteFunction in unity? It seems that the FunctionArgument inside request throws the error. Besides, you may refer to this link: Calling your function from Unity and PlayFab CloudScript Context, Variables and Server SDKs

0 Likes 0 ·
Murtadha avatar image Murtadha Simon Cui commented ·
     public void CallAzureFunction()
     {
         ExecuteFunctionRequest cloudFunction = new ExecuteFunctionRequest()
         {
             FunctionName = "DeleteYourAccount",
         FunctionParameter = new { loggedInPlayfabId = PlayerPrefs.GetString("Player ID")},
         GeneratePlayStreamEvent = true
         };
         PlayFabCloudScriptAPI.ExecuteFunction(cloudFunction, FunctionSuccess, Fail);
    
     }
    
     private void Fail(PlayFabError obj)
     {
         Debug.Log("failed:" + obj.Error);
     }
    
     private void FunctionSuccess(ExecuteFunctionResult obj)
     {
         Debug.Log("Success:" + obj.FunctionResult);
     }

This is the code I'm using to call the function. One thing I forgot to mention is the error I'm getting: "Exception while executing function: HttpExample. Azure Function: Object reference not set to an instance of an object" is in visual studio code after I start debugging my code. if this info helps with anything.

0 Likes 0 ·
Simon Cui avatar image Simon Cui Murtadha commented ·

Hi, I tested your codes in my azure function using Vscode, which worked fine and deleted a master account. Have you tried local debug your codes in Vscode? Please refer to: Tutorial: Local debugging for Cloudscript using Azure Functions and Code and test Azure Functions locally. Please let me know if you encounter any question further.

0 Likes 0 ·
Show more comments
Murtadha avatar image Murtadha commented ·

Yes. I have. I tested deleting a players account using a specific master player id and the code worked with no issues. My only problem is when I try passing the master id from unity I get this error.

0 Likes 0 ·
Simon Cui avatar image Simon Cui Murtadha commented ·

When you debug your codes in VScode, you may check if the context contains FunctionArgument or "loggedInPlayfabId". Have you tested the other sample Azure Functions like “hello world" called from Unity without passing any parameters? If so, did them work well?

0 Likes 0 ·
Murtadha avatar image Murtadha Simon Cui commented ·

So I'm getting the same error when using the workaround method. when I'm debugging my code I'm getting context being null. is that normal? And I have tried other azure functions and my own too without passing parameters and it had been called successfully from unity. 5512-02.jpg5513-01.jpg

0 Likes 0 ·
02.jpg (84.7 KiB)
01.jpg (45.1 KiB)

1 Answer

·
Simon Cui avatar image
Simon Cui answered

The Context should not be null. Sorry for mentioning that workaround using PlayerPlayStreamFunctionExecutionContext. In your case, the FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync()); is the correct Context Model via ExecuteFunction API, which is used in your original version. As shown in the screenshot below, you may try PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId inside the DeleteMasterPlayerAccountAsync task. You can also see that the testPlayFabIdWithProfile is as same as PlayFabId from args.loggedInPlayfabId.
5525-01.png


01.png (232.7 KiB)
2 comments
10 |1200

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

Murtadha avatar image Murtadha commented ·

The context is still null even while using the older context model:

 FunctionExecutionContext&lt;dynamic&gt; context = JsonConvert.DeserializeObject&lt;FunctionExecutionContext&lt;dynamic&gt;&gt;(await req.ReadAsStringAsync());

So I think this is the main issue why the code is not working. it is not the case of how I'm getting the PlayerId, it's that the context is still null even after I'm giving it a value. Could there be some outside of the code issue? like faulty extensions or some permissions I missed? Thanks again for bearing with me :) 5533-32313.jpg

0 Likes 0 ·
32313.jpg (72.2 KiB)
Simon Cui avatar image Simon Cui Murtadha commented ·

You may try to step over to next line of codes, then check the context again :). Besides, you can use Postman (REST) to make a quick API call for testing.

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.