Command Batching
Batch commands for server-side processing to optimize network efficiency, reduce overhead, and improve performance.
Read time 6 minutesLast updated 17 hours ago
Command batching is the concept where each game action is a Command which can be collected into a queue to be sent to the server in batches for processing. Command batching optimizes your game’s bandwidth to be as energy efficient as possible, and prevent poor performance due to frequent server calls or rate limiting. This provides a smoother game experience with less downtime.

- The first action results in 100 XP and 10 Coins.
- The second action results in 50 XP and 5 Coins.
- The third action results in 200 XP and 30 Coins.
Prerequisites
To use this sample use case, you must download and install the UGS Use Cases project in your Unity project.Overview
To see this use case in action, open the samples menu and navigate to Command Batching. To open this scene directly and interact with the use case:- In the Unity Editor Project window, select Assets > Use Case Samples > Command Batching, then double-click to open the sample scene.
CommandBatchingSample.unity - Press Play to enter Play mode.
Initialization
TheCommandBatchingSceneManager.csStart- Initializes Unity Gaming Services.
- Signs in the player anonymously using the Authentication service. If you’ve previously initialized any of the other sample scenes, Authentication will use your cached Player ID instead of creating a new one.
- Retrieves and updates the player's currency balances from the Economy service.
- Initializes the game interface and buttons.
Functionality
Player turns
In this sample, a single "game" involves making a fixed number of turns, where a single turn is completed by clicking on one of four buttons. Each button represents a simulated action with corresponding rewards. When you click on one of the action buttons, the following occurs:-
The button’s method calls the
OnClickscript, which consumes a turn.CommandBatchingSceneManager.cs - The script then generates a command associated with the action you clicked and sends it to the batch queue for future server processing. Each command uses Remote Config values to map rewards for the corresponding action.
-
It also calls that action’s associated script to distribute rewards to the client. Note: Local reward distribution allows players to immediately see the results of the turn, but is not server-authoritative. The server overwrites any local distributions after it validates and processes all commands at the end of the game.
CommandSystem - Finally, the calling script checks to see if the player is out of turns, in which case it triggers the game-over condition.
Gave over
When the player has zero turns remaining, the game is over. The following occurs:- The client adds a final game over command to the batch queue.
- The client sends the entire command queue to Cloud Code as a single JSON.
- The Cloud Code script unpacks the JSON blob into a list of commands again, then verifies that the batch contains the exact number of expected commands in a valid order (for example, the player cannot trigger the Achieve Bonus Goal command until the Open Chest command occurs).
CommandBatch_ProcessBatch.js - If the batch is valid, the Cloud Code script retrieves the rewards to distribute from Remote Config values then issues them directly through the Economy (currencies) and Cloud Save (XP and ) services. If the batch is invalid, the script returns an error and rewards will not be distributed on the server.
GoalsAchieved
GoalsAchievedSetup
Requirements
To replicate this use case, you'll need the following Unity packages in your project:Package | Role |
|---|---|
| Authentication | Automatically signs in the player as an anonymous user to keep track of their data server-side. |
| Cloud Code | Processes queued commands at the end of a game. |
| Cloud Save | Stores the mapping between commands and their corresponding rewards, which the client and Cloud Code references to distribute rewards. |
| Deployment | Provides a cohesive interface to deploy assets for Cloud Services. |
| Economy | Retrieves the player's starting and updated currency balances at runtime. |
| Remote Config | Maintains key-value stores for player stats, such as XP and Goals Achieved. |
Unity Cloud services configuration
To replicate this sample scene's setup in your own Unity project, configure the following items:- Cloud Code scripts
- Economy items
- Remote Config values
Using the Deployment package
To deploy configurations using the Deployment package:- Open the Deployment window.
- Check in and
Common.Command Batching - Click .
Deploy Selection
Using the Unity Dashboard
You can use the Unity Dashboard to manually configure your services by project and environment. Refer to the following sections to configure this sample.Cloud Code
Publish the following script in the Unity Dashboard:Script | Parameters | Description | Location in project |
|---|---|---|---|
|
| Processes queued commands at the end of a game. | |
Cloud CodeEconomy
Configure the following resources in the Unity Dashboard:Resource type | Resource name | ID | Description |
|---|---|---|---|
| Currency | Coin | | A reward for certain actions. |
| Currency | Gem | | A reward for certain actions. |
Remote Config
Set up the following config values in the Unity Dashboard:Key | Type | Description | Value |
|---|---|---|---|
| JSON | Maps the |
|
| JSON | Maps the |
|
| JSON | Maps the |
|
| JSON | Maps the |
|
| JSON | Maps the game over command (no more turns remain) to its corresponding rewards. |
|