Trigger structure
A trigger configuration defines what should be invoked when an event is fired.
It may look like this:
{
"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'"
}
name
: The name of the trigger. It must be unique per project.eventType
: Used to match the trigger to an event. It is composed of theeventName
andeventVersion
fields, corresponding to theeventName
andpayloadVersion
fields in the event configuration.actionUrn
: Defines what should be invoked when the trigger is fired. For example,urn:ugs:cloud-code:TestScript
invokes theTestScript
Cloud Code script, orurn:ugs:cloud-code:TestModule/TestFunction
invokes theTestFunction
function in theTestModule
Cloud Code module.actionType
: The type of the action to be executed when the trigger is fired. Currently, onlycloud-code
is supported.filter
: An optional field that defines a condition that must be met for a trigger to fire. If the condition isn't met, the trigger doesn't fire. For more information, refer to Filters.
Note: You cannot create triggers with filters through the CLI. Use the Triggers API or the Unity Cloud Dashboard.
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 eventName
and payloadVersion
configuration.
Note: The eventType
maps an event to a trigger.
Scheduled 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 eventType
field in the trigger configuration to the eventName
and payloadVersion
fields in the schedule configuration.
When 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 my-script
Cloud Code script.
Multiple events per trigger
You can associate the same event with multiple triggers.
Note: The current limit for the number of triggers per event type is 32.
For example, you can create two triggers that invoke when a user signs up.
The first trigger invokes the add-cloud-save-data
script to add a Cloud Save data entry for the authenticated user:
{
"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 add-economy-items
endpoint from Economy
module to add a virtual item to the authenticated user:
{
"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 are fire and invoke the associated Cloud Code resources.