Publish events to Triggers
Run scripts, module functions, or webhooks through Triggers when your Cloud Code C# module publishes a custom event.
Read time 5 minutesLast updated 2 days ago
A Cloud Code C# module can publish its own events, called custom events, to the Triggers service. You choose the event name and payload, and a trigger on that event runs a Cloud Code script, a module function, or a webhook.
Custom events let a function move follow-up work to a separate call so the player doesn't wait for that work to finish. For example, a function that ends a match can publish a event. A trigger on that event can then grant rewards in a separate call.
match-endedPrerequisites
Your module needs one of the following, depending on how you build it:
- For Unity Cloud Code modules (.ccmu), the Cloud Code package () version 3.1.0 or later.
com.unity.services.cloudcode - For modules in a separate C# solution, for example a solution linked through a module reference file, the package version 0.0.29 or later. To check the version, open the module's .csproj file.
Com.Unity.Services.CloudCode.Apis
Publish an event
Cloud Code provides the interface, so you don't need to register the interface in your module setup.
ITriggersEventClientWhen a function in a stateful module publishes an event, the event has the module's scope, such as or . For how scope affects which module functions a trigger can call, refer to Scoped trigger events.
PlayerMultiplayerSessionTo publish an event, follow these steps:
-
Add anparameter to a
ITriggersEventClientmethod.CloudCodeFunction -
Callwith the execution context, a short event name, and an optional payload. For the rules that the name and payload follow, refer to Name the event and set the payload.
PublishEventAsyncThe following example publishes aevent with the match ID and score.match-endedusing System.Collections.Generic;using System.Threading.Tasks;using Unity.Services.CloudCode.Apis.Triggers;using Unity.Services.CloudCode.Core;namespace MatchExample{ public class MatchModule { [CloudCodeFunction("EndMatch")] public async Task<string> EndMatch(IExecutionContext context, [CloudCodeService] ITriggersEventClient triggersEventClient, string matchId, int score) { var payload = new Dictionary<string, object> { { "matchId", matchId }, { "score", score } }; return await triggersEventClient.PublishEventAsync(context, "match-ended", payload); } }}
After you call , the returned task completes when Cloud Code accepts the event and returns the full event type, such as . Triggers receives the event asynchronously, so a completed task doesn't mean a trigger has run.
PublishEventAsynccom.unity.services.cloud-code.custom.match-ended.v1If throws an , check the event name and payload against the rules. An means Cloud Code refuses or can't send the event, for example because the event exceeds the event chain limit.
PublishEventAsyncArgumentExceptionInvalidOperationExceptionCloud Code builds every event type in the following format.
com.unity.services.cloud-code.custom.<event-name>.v1
Name the event and set the payload
The event name and payload follow these rules:
- The event name can have up to 64 characters and can contain only lowercase letters from to
a, digits fromzto0, hyphens (9), and underscores (-)._ - Pass the short event name only, such as . Cloud Code adds the namespace and version.
match-ended - Cloud Code serializes the payload to JSON. The JSON can be up to 10 KB.
- To publish an event without data, pass as the payload or omit the payload.
null
A trigger that uses an event depends on the event's payload keys. You can add keys at any time. To remove or rename a key, publish the event under a new name.
Respond to the event
To run an action when a module publishes the event, create a trigger on the event's full event type.
The following trigger configuration runs the function of the module each time a module publishes . For the fields and the ways to create a trigger, refer to Trigger structure and Define triggers.
GrantMatchRewardsMatchRewardsmatch-ended{ "name": "grant-match-rewards", "eventType": "com.unity.services.cloud-code.custom.match-ended.v1", "actionType": "cloud-code", "actionUrn": "urn:ugs:cloud-code:MatchRewards/GrantMatchRewards"}
The Triggers service passes the payload to the action as parameters, so receives and by name. A JavaScript script receives only the payload keys that it declares as parameters.
GrantMatchRewardsmatchIdscoreTo run the trigger for some events only, add a filter on the payload. A filter fails on an event that doesn't contain the key that the filter reads, so check for the key first. Add the following field to the trigger configuration.
filter"filter": "'score' in data && data['score'] >= 100"
After you create the trigger, the Triggers service runs each time a module publishes with a score of 100 or more.
GrantMatchRewardsmatch-endedTo create the trigger in the Unity Dashboard, refer to Add a Cloud Code custom event.
Test locally
From Cloud Code package version 3.1.1, the local Cloud Code server supports . The local server checks each event the same way Cloud Code does, then logs the event instead of sending the event to Triggers.
ITriggersEventClientTo test that a function publishes a valid event, follow these steps:
- Run the module on the local server. For more information, refer to Execute modules on the local server.
- Call the function that publishes the event.
- Open the local server's logs. For more information, refer to Find the local server's logs and data.
After you open the logs, the log entries show the full event type, scope, and payload of each event that passes the checks. If the event isn't valid, the function throws the same exception that it throws in the cloud.
The local server doesn't run triggers. To test the trigger itself, use a cloud environment.