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# modulesWrite 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 scriptsWrite 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. |
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 thecontextIExecutionContextplayerIdaccessTokenunityInstallationIdanalyticsUserIdcorrelationId
Cross-player data
To modify cross-player data using Cloud Code, you need to authenticate using theserviceTokenPayload 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 includeprojectIdenvironmentIdcorrelationIdnameJavaScript
or a module has a functionmodule.exports = async ({ params, context, logger }) => { return "Hello " + params.name;};
SayHellonamethen you would define the payload for the schedule configuration as follows:using Unity.Services.CloudCode.Core;namespace ExampleModule;public class MyModule{ [CloudCodeFunction("SayHello")] public string Hello(string name) { return $"Hello, {name}!"; }}
When the trigger is fired, the Cloud Code script or module is executed with the payload parameters populated.{ "name": "hello-world-test", "eventName": "hello-world-test", "type": "one-time", "schedule": "2023-08-28T15:20:00Z", "payloadVersion": 1, "payload": "{\"name\": \"World\"}"}