CloudScript basics
The key to getting the most out of CloudScript is knowing how to work with the inputs you have available – namely, the args and context for your handler. For example, here’s thehelloWorld example from the starter CloudScript, loaded as Revision 1 in all newly created titles (also available in our GitHub, shown below).
inputValue, and uses the value for that key as part of the text returned in the debug log info for the execution.
The context input parameter
However, it’s also possible to call a CloudScript as a result of an event in PlayStream, via a Rule (Automation->Rules), a Segment Enter/Exit Action (Players->Segments), or the Task Service (Automation->Tasks). When you do, the context passed in to the function provides all the information you’ll need to take the appropriate action. To read about the basics of how PlayStream events work, see our blog Introducing PlayStream, and for a list of PlayStream event types and their properties, see our PlayFab API Reference. To view this action, have a look at thehandlePlayStreamEventAndProfile handler from the same sample CloudScript.
-
There’s the
playStreamEvent, which you can see in the example code above. TheplayStreamEventcontains the complete event which triggered the handler as a JSON object, with all the parameters you see in the PlayStream event documentation. So for example, if you set up a rule in your title that calledhandlePlayStreamEventAndProfileon anyplayer_logged_in event,playStreamEvent.EventNamewould beplayer_logged_in, etc. (here’s the complete set of parameters for that event). -
Next, there’s the
playerProfile, also shown in the previous example. This contains information about the player that triggered the event. You can find all the details of the profile parameters here, but among other things, it contains the complete set of statistics for the player in your title, and any custom tags you have assigned to the player, so that you can use that data for rich decision-making. -
The last element of context is
triggeredByTask. Unlike the first two, which are set when using Rules and Segment Enter/Exit triggers,triggeredByTaskis 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.
playerProfile, but you won’t have a playStreamEvent.
And for a task that’s simply run against your game but without any segment, there won’t be a playerProfile, since the intent is to run something more general, like setting some title data for an event.
So name is the element you’ll want to use to use in your handler’s code flow, to determine the appropriate action to take.
PlayStream plus CloudScript
In many ways, CloudScript handlers triggered by PlayStream actions have even more potential functionality than those triggered directly through calls toExecuteCloudScript, since there’s a rich set of data made available via the context.
This gives you the ability to update your handlers post-launch with more logic that makes use of elements of the event or player profile that you hadn’t originally anticipated, without needing to update your client code in any way.
In addition, we’ll continue to make additions to the player profile in future updates of the PlayFab service, which will provide even more options for server-side logic.