question

guillaumedanel avatar image
guillaumedanel asked

SetObjects in a cloud function

Hi,

I am using cloud functions using C# with Unity. Having followed the cloud functions tutorial, I am using the following line to get the context of the call at the start of my cloud function:

FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

This way is very convenient because I can pass a C# object as a parameter and get it back in the cloud function.

However I am trying to set objects on my player data and the only example I found requires a FunctionContext to work (see MakeEntityApiCall):

/* Use the ApiSettings and AuthenticationContext provided to the function as context for making API calls. */
var dataApi = new PlayFabDataInstanceAPI(context.ApiSettings, context.AuthenticationContext);
/* Execute the entity API request */
var setObjectsResponse = await dataApi.SetObjectsAsync(setObjectRequest);

So my question is:

what is the best way to set objects on the player data overall?

Should I get the FunctionExecutionContext to read the argument as a class instance, but also get the FunctionContext to be able to use the PlayFabDataInstanceAPI?

P.S.: on a side note, when I try to use

var context = await FunctionContext<dynamic>.Create(req);

I get an error saying FunctionContext does not exist and VS Code doesn't suggest to add any using. How is that possible?

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

·
Sarah Zhang avatar image
Sarah Zhang answered

You needn’t use both FunctionExecutionContext and FunctionContext to write the Azure Functions. This FunctionContext used in the example you found comes from this script-- PlayFabFunctionContexts.cs. The features of the PlayFabFunctionContexts.cs and the features of CS2AFHelperClasses.cs overlap each other. If you use the CS2AFHelperClasses.cs in the project, you need to modify the example’s code to make it work. Or you can import the PlayFabFunctionContexts.cs to fix the "couldn't be found" error.

Besides, as the comment of PlayFabFunctionContexts.cs said, we don't suggest to use this plugin with other production ready code. We suggest to use CS2AFHelperClasses.cs in the production environment.

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.

guillaumedanel avatar image guillaumedanel commented ·

Thank you very much! I'm happy to use CS2AFHelperClasses.cs and the FunctionExecutionContext. Using it how do I call SetObjects on the player data entity? Because as mentioned in the original post the only code I found doing that using the FunctionContext which has ApiSettings and AuthenticationContext information necessary to create the PlayFabDataInstanceAPI.

0 Likes 0 ·
guillaumedanel avatar image guillaumedanel commented ·

Looking at the code inside PlayFabFunctionContexts.cs it was easy to recreate the API settings and authentication context necessary to get the PlayFabDataInstanceAPI. 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.