Trigger structure
Understand the structure of a trigger configuration that defines what to invoke when an event fires.
Read time 3 minutesLast updated 4 months ago
A trigger configuration defines what should be invoked when an event is fired. The structure differs depending on whether the action is Cloud Code or a webhook.
Cloud Code trigger
A Cloud Code trigger invokes a script or module when the event fires. It uses and an that identifies the script or module.
actionType: cloud-codeactionUrn{ "name" : "example-trigger", "eventType" : "com.unity.services.{{SERVICE_NAME}}.{{EVENT_NAME}}.v{{EVENT_VERSION}}", "actionUrn" : "urn:ugs:cloud-code:{{SCRIPT}}", "actionType" : "cloud-code", "filter": "data['parameter'] == 'value'"}
- : The name of the trigger. It must be unique per project.
name - : Matches the trigger to an event. It is composed of the
eventTypeandeventNamefields, corresponding to theeventVersionandeventNamefields in the event configuration.payloadVersion - : What is invoked when the trigger fires. For example,
actionUrninvokes theurn:ugs:cloud-code:TestScriptCloud Code script, orTestScriptinvokes theurn:ugs:cloud-code:TestModule/TestFunctionfunction in theTestFunctionCloud Code module.TestModule - : Set to
actionTypefor Cloud Code triggers.cloud-code - : Optional. A condition that must be met for the trigger to fire. For more information, refer to Filters.
filter
Scoped trigger events
Each Cloud Code event and module can declare a scope type, such as , that indicates the context in which the action runs. To configure a trigger with a scoped module action, the scope types need to be compatible. The following rules apply when you select a module endpoint:
Player- If the endpoint has no scope type, you can use the endpoint regardless of the event's scope type.
- If the endpoint declares a scope type, the event scope type must be the same.
Event | Service | Scope type |
|---|---|---|
| Authentication | |
| Authentication | |
| Cloud Save | |
| Leaderboards | |
| Leaderboards | None |
| Moderation | |
For more information about scoped Cloud Code events, refer to Stateful Cloud Code.
Webhook trigger
A webhook trigger sends an HTTP request to an external URL when the event fires. The webhook trigger configuration requires the following fields:
actionType: webhookactionUrn: urn:ugs:webhook- : An object containing the request details:
webhook- (Required): The destination URL.
url - (Optional): The HTTP method (for example
methodorPOST). Defaults toGET.POST - (Optional): Custom key-value pairs for the request.
headers - (Conditional): The request body template. Required for all content types except
payloadTemplate. Forapplication/json, you may omit it to forward the full event payload as-is.application/json
You can inspect and replay failed webhook deliveries from the dead letter queue.
{ "name" : "example-webhook-trigger", "eventType" : "com.unity.services.leaderboards.score-submitted.v1", "actionUrn" : "urn:ugs:webhook", "actionType" : "webhook", "webhook": { "url": "https://example.com/events", "method": "POST", "headers": { "Content-Type": "application/json" }, "payloadTemplate": "{\"event\": \"score-submitted\", \"data\": {{.}}}" }}
For full webhook configuration and template syntax, refer to Webhooks.
Overlap with events
Triggers are associated with events. When an event is fired, the associated trigger is invoked. The event and the trigger must share the same and configuration.
eventNamepayloadVersionScheduled events
Given the following schedule configuration:
{ "name" : "recurring-schedule", "eventName": "example-event", "type": "recurring", "schedule": "0 0 * * *", "payloadVersion": 1, "payload": "{\"someBoolean\": true, \"someString\": \"something\"}"}
And the following trigger configuration:
{ "name" : "example-trigger", "eventType" : "com.unity.services.scheduler.example-event.v1", "actionUrn" : "urn:ugs:cloud-code:my-script", "actionType" : "cloud-code"}
You can associate the trigger with the event type by setting the field in the trigger configuration to the and fields in the schedule configuration.
eventTypeeventNamepayloadVersionWhen the schedule configuration is deployed, the schedule is triggered at the specified time, sending an event to the Triggers service, which in turn invokes the associated Cloud Code resource.
UGS events
Given you want to associate a trigger with an event type from UGS, you need to specify the event type in the trigger configuration.
For example, if you want to fire a trigger on the Authentication: Signed Up event, you can create the following trigger configuration:
{ "name" : "example-trigger", "eventType" : "com.unity.services.player-auth.signed-up.v1", "actionUrn" : "urn:ugs:cloud-code:my-script", "actionType" : "cloud-code"}
Every time a user signs up, the trigger is fired, invoking the Cloud Code script.
my-scriptMultiple events per trigger
You can associate the same event with multiple triggers.
For example, you can create two triggers that invoke when a user signs up.
The first trigger invokes the script to add a Cloud Save data entry for the authenticated user:
add-cloud-save-data{ "name" : "auth-cloud-save", "eventType" : "com.unity.services.player-auth.signed-up.v1", "actionUrn" : "urn:ugs:cloud-code:add-cloud-save-data", "actionType" : "cloud-code"}
The second trigger invokes the endpoint from module to add a virtual item to the authenticated user:
add-economy-itemsEconomy{ "name" : "auth-economy", "eventType" : "com.unity.services.player-auth.signed-up.v1", "actionUrn" : "urn:ugs:cloud-code:Economy/add-economy-items", "actionType" : "cloud-code"}
Every time a user signs up, both triggers fire and invoke the associated Cloud Code resources.