Documentation

Support

Set up Cloud Code

Set up Cloud Code scripts or modules to use with the Triggers service.
Read time 2 minutesLast updated a day ago

To make use of the Triggers service, you should define a Cloud Code script or a Cloud Code module.
Cloud Code C# modules
Write server-side code with a type-safe programming language and powerful .NET components. Consume other content needed in the project bundling dynamic link libraries (DLLs) with tools like NuGet.Get started with Cloud Code C# modules.
Cloud Code JavaScript scripts
Write server-side code with a easy to iterate language. Take the advantage of using the same ecosystem if you come from a JavaScript frontend background. Bundle your code with reusable pieces through CommonJS and ECMAScript module systems supported out-of-the-box with the Cloud Code command line.Get started with Cloud Code JavaScript scripts.
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
context
object for scripts, and the
IExecutionContext
interface for modules during execution.
Due to lack of player authentication, the following attributes are not available:
  • playerId
  • accessToken
  • unityInstallationId
  • analyticsUserId
  • correlationId
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
serviceToken
and call out to other UGS services.
You 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
projectId
,
environmentId
and
correlationId
.
For example, given a script below has a string parameter
name
:

JavaScript

module.exports = async ({ params, context, logger }) => { return "Hello " + params.name;};
or a module has a function
SayHello
with a string parameter
name
:
using 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.