Set up Cloud Code
Set up Cloud Code scripts or modules to use with the Triggers service.
Read time 2 minutesLast updated 4 months ago
To make use of the Triggers service, you should define a Cloud Code script or a Cloud Code module.
This script or module is executed when a trigger is fired, given you have defined a trigger configuration.
Execution return value
Currently, Triggers service does not interact with any return value coming from Cloud Code.
Account for this by creating modules and scripts that execute your game logic directly.
Context attributes
Since the Triggers service is authenticated by a Service Account, certain attributes are not available in the object for scripts, and the interface for modules during execution.
contextIExecutionContextDue to lack of player authentication, the following attributes are not available:
playerIdaccessTokenunityInstallationIdanalyticsUserIdcorrelationId
Ensure your scripts and modules do not rely on these attributes.
Cross-player data
To modify cross-player data using Cloud Code, you need to authenticate using the and call out to other UGS services.
serviceTokenYou cannot retrieve the player ID from the context object when writing Cloud Code logic.
However, when triggers are paired with events emitted from UGS, you can often retrieve a player ID from the event payload and interact with player data.
Refer to Supported UGS events for more information on supported events and their payloads.
You can also call out to other Unity Gaming Services to retrieve player data. For example, you can retrieve players from Leaderboards or players in the Lobby service by calling out to these services in your script or module.
Refer to Use case sample: Reward top players with in-game currency for an example.
Payload parameters
Cloud Code modules and scripts can be invoked with a set of parameters. These parameters are defined in the event’s payload.
The scheduled events' payloads are defined by the user in the schedule configurations. For UGS events, refer to Supported UGS events for the contents of the event payloads.
The parameters should be defined in the same way as they would be for any other script or module.
The triggering event’s payload is used as the argument set for the Cloud Code invocation; any defined parameters that match keys in the event payload are populated.
All event payloads automatically include , and .
projectIdenvironmentIdcorrelationIdFor example, given a script below has a string parameter :
nameJavaScript
module.exports = async ({ params, context, logger }) => { return "Hello " + params.name;};
or a module has a function with a string parameter :
SayHellonameusing Unity.Services.CloudCode.Core;namespace ExampleModule;public class MyModule{ [CloudCodeFunction("SayHello")] public string Hello(string name) { return $"Hello, {name}!"; }}
then you would define the payload for the schedule configuration as follows:
{ "name": "hello-world-test", "eventName": "hello-world-test", "type": "one-time", "schedule": "2023-08-28T15:20:00Z", "payloadVersion": 1, "payload": "{\"name\": \"World\"}"}
When the trigger is fired, the Cloud Code script or module is executed with the payload parameters populated.