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."
Answer by Brendan · Oct 06, 2017 at 08:46 AM
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.
Answer by larissa · Oct 07, 2017 at 10:47 PM
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:
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?
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.
Answer by Guillermo Eduardo Sanfiz · Dec 12, 2021 at 12:02 PM
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???
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;
cloud script handlers 1 Answer
server.DeletePlayer is not a method 1 Answer
Steam User Info and DLCs 1 Answer
CloudScript don't have api call documentation & uncomplete CloudScript.d.ts ? 2 Answers
Can Cloud Code Access the Admin API 1 Answer