question

Faithful David avatar image
Faithful David asked

,PlayFab Cloudscript using azure functions,

,

Hi,

I'm new to playfab and azure functions.

I'm trying to create an azure function app for my game and trying to test it locally. So far, I've been able to create the app using visual studio 2019 but getting it to work locally has been a pain in the ass.

I followed a tutorial on GitHub https://github.com/PlayFab/PlayFab-Samples to setup the local debugging environment. I've added the ExecuteFunction.cs class to the root of the project. I'm suppose to add CS2AFHelperClasses.cs somewhere in the project? I'm not sure where or why.

I managed to get it to barely work and when I tested it, I noticed something: I cannot use a struct as function parameter or my function parameter (class) must not contain a struct else it'll throw an exception. Is this normal?

Also I'm not sure how to go about writing the functions. I can't find any sort of template online and the default one that comes when you create a new project seems to be for Asp.Net style. Can anybody help?

10 |1200

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

onedevapp avatar image
onedevapp answered

ExecueFunction.cs and CS2AFHelperClasses.cs are two different classes for two different purposes.

"Should I remove the ExecueFunction.cs class from the project? I thought that was what will help me to test locally?" - Yes, ExecueFunction.cs will help u test locally and u need to add the same in the root folder of your Function App.

"it is highly recommended to import CS2AFHelperClasses.cs because it does handle the function request, with parameter." - Yes, CS2AFHelperClasses.cs is like a wrapper class for shared context model and u need to add the same in the root folder of your Function App.

One can achieve the Function App with or without the CS2AFHelperClasses.cs.

  • Hello World Example with CS2AFHelperClasses.cs
FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

dynamic args = context.FunctionArgument;
  • Hello World Example with out CS2AFHelperClasses.cs
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);

Also you can deploy and publish CS2AFHelperClasses.cs to the Azure Function App cloud but ExecueFunction.cs, you shouldn't.

"I'm not sure how to go about writing the code for these different scenarios because there's no example code for the different scenarios." - Execution of a script can occur through several methods (APIs, Scheduled Tasks, PlayStream Event, Segment Entering and Exit method). The context of the execution is important to implement your CloudScript. So basically calling Azure functions or cloud scripts functions falls in either one of these.

  • ExecuteFunction API call
  • Rule triggered function
  • Scheduled Task triggered function

"I'll create the function app, deploy it, make a rule in playfab saying when the event player_added_title occurs, call my function (say "AddSomeData")." - Can be done as following.

  • Create function app as SingupBonus
  • Add amount to this user using ServerAPI (example from TicTacToe)
  • Deploy and Publish to Azure Function and Register the URL in the Playfab Functions
  • Set this as event for player_added_title in Rule.

Goodluck.

10 |1200

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

Faithful David avatar image
Faithful David answered

So you're saying that the test version on GitHub is outdated? Should I remove the ExecueFunction.cs class from the project? I thought that was what will help me to test locally?

I tried tried the sample using the link you provided and it seems to work but I still don't understand the different context models. I've been reading the documentation and there's a place that says that the way you write the code depends on how the function will be executed, either from client code, or playstream events etc. I'm not sure how to go about writing the code for these different scenarios because there's no example code for the different scenarios. For example, let's say I want to add some specific data to the player internal data and player readonly data when a player creates an account. I'll create the function app, deploy it, make a rule in playfab saying when the event player_added_title occurs, call my function (say "AddSomeData"). How do I go about writing this function? Which of the context models will I use? How will I get the playfabID of the player to make server api calls?

Please I just need a few sample code to show these different scenarios in c#. This thing is really delaying my progress.

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.

Seth Du avatar image Seth Du ♦ commented ·

There are 3 basic context types, which are determined by how this azure function is triggered.

  • ExecuteFunction API call
  • Rule triggered function
  • Scheduled Task triggered function

All those contexts are provided in the official document -- PlayFab CloudScript using Azure Functions Context Models - PlayFab | Microsoft Docs. It is also recommended to output the context on your own (plaint string should be enough, it is in JSON format), so that you can differ the properties included.

First is to verify the caller, checking if this function is triggered by a proper source, for example, an event triggered function shouldn't be executed by a direct ExecuteFunction API call. Then you may call any PlayFab API calls in your function. Secret key can be stored in configuration settings of Azure Portal, and you can call GetEntityToken API then define PlayFabServerInstanceAPI for Server API.

>>How will I get the playfabID of the player to make server api calls?

Caller information is contained in the context (you may check CallerEntityProfile property), if it is event triggered, event playload is also contained.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ commented ·

Sorry for the confusion. If you are looking at ExecuteFunction.cs file from Local debugging for Cloudscript using Azure Functions - PlayFab | Microsoft Docs, it doesn't mean out of date and CS2AFHelperClasses.cs file has different purposes. Please refer to @onedevapp answer. Thanks.

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.