question

larissa avatar image
larissa asked

Questions on the context parameter

I've recently discovered the existence of the context parameter in cloudscripts and been using it, to make sure certain handlers would only be called from tasks:

handlers.MyHandler= function(args, context)
{
 	// Don't allow this to be triggered by anything but a task
	if (context.hasOwnProperty("triggeredByTask") && context["triggeredByTask"].hasOwnProperty("Name"))
	{
		// Do stuff here ...

	}
}

1.) When using above code, I've noticed the context parameter will not be provided / null for tasks that are run on a player segment? It's been working for the "run cloudscript function once" type of task though.

a) Is this intended? The name "triggeredByTask" certainly suggests, that it would be possible to check it for either type of task.

b) Is there a different way to differentiate between the client calling the handler by itself vs. a task calling the handler for all players in a segment? (Of course there's always the possibility to hand an additional "password-parameter" to the task and check for that, but maybe there's a more elegant way than that?)

2.) Is there any documentation for the context parameter other than the brief mentions in this tutorial (https://api.playfab.com/docs/tutorials/landing-automation/writing-custom-cloud-script), which says

"context is an advanced parameter. In this example, it is null. See this guide(link pending) for more information.This parameter is server-controlled and safe."

CloudScripttasksdocumentation
10 |1200

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

brendan avatar image
brendan answered

Yes, the Context is available for PlayStream Action-originating Cloud Script. Here's a tutorial that details this: https://api.playfab.com/docs/tutorials/landing-automation/using-cloud-script-actions-with-playstream. You can also, in the Action definition, specify static data that you pass in as arguments to the Cloud Script, so you could technically also use a secret key system there to provide an argument that couldn't possibly be sent by a client, since they have no access to the PlayStream Action defniitions.

10 |1200

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

larissa avatar image
larissa answered

Sorry, I'm not sure if I'm understanding this correctly yet:

The tutorial you linked states:

"Now, the last element of context is triggeredByTask. Unlike the first two, which are set when using Rules and Segment Enter/Exit triggers, triggeredByTask is only applicable when the handler is running as a result of a Task, whether manual or on a timer. It contains only two parameters:

  • Name – The unique name you gave your Task when you created it
  • Id – The unique identifier automatically generated by PlayFab for your Task

For a Task run against a user Segment, you’ll also have the playerProfile, but you won’t have a playStreamEvent."

Doesn't this imply, that the context-property "triggeredByTask" is supposed to be available for tasks that are run against a user segment? However when I tried it, it was null. Specifically the following handler

handlers.MyHandler = function (args, context)
{
	logd("MyHandler context: " + JSON.stringify(context));
	...  

when run by a scheduled task against a segment, would produce the following output:

"MyHandler context:
{
    \"playerProfile\":
    {
        \"PlayerId\":\"5X1BD226109B0211\",
        \"TitleId\":\"6131\",
        ...
    },
    \"playStreamEvent\":null,
    \"triggeredByTask\":null
}"

So even though this was triggered by a task, triggeredByTask is null. Is this intended?

1 comment
10 |1200

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

brendan avatar image brendan commented ·

Thanks for calling that out. I just tested and reproduced this (the triggeredByTask not being available in the context). I've opened a bug on that to get it fixed asap.

1 Like 1 ·
Guillermo Eduardo Sanfiz avatar image
Guillermo Eduardo Sanfiz answered

I can not belive that now I can not write code directly in playfab, now I must use Azure and the doc is terrible!! It is not very sure how to get the name arg, the code works calling the function in the navigator, but via the playfab api it fails. Why you (microsoft) made that change???

1 comment
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 ·

Details of context model are listed in PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Docs and you may directly import CS2AFHelperClasses.cs to your project.

Please follow the document -- PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Docs and use the following code to use argument.

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

dynamic args = context.FunctionArgument;
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.