Client bindings in CloudBehaviours
Understand the generated component that connects your game to a CloudBehaviour, and how it resolves its scope and reports its state.
Read time 4 minutesLast updated 2 days ago
The client bindings are a generated component that connects your game to a CloudBehaviour. The component exposes the CloudBehaviour's RPCs, synchronized state, and push channels as members you can use from your game. It also resolves which scope instance to connect to, and raises lifecycle events that report its connection state.
MonoBehaviourFor a walkthrough that sets up client bindings in a scene, refer to Get started with CloudBehaviours.
Client bindings script
When you create a CloudBehaviour, the Editor creates a client bindings script in the module's folder. The script is a partial class marked with the attribute:
ClientMonoBehaviour[CloudBehaviour][CloudBehaviour(typeof(MyCloudBehaviour))]public partial class MyCloudBehaviourClient : MonoBehaviour{ protected virtual void OnEnable() => EnableClient(); protected virtual void OnDisable() => DisableClient();}
The Editor generates the rest of the partial class each time the cloud script compiles. The generated part contains the following members:
- A method for each RPC.
- A property for each synchronized field or property, and for each per-player state view declared with .
[SyncView] - A for each push channel that the cloud class declares.
UnityEvent - The ,
ScopeChanged,Synchronized,MessageReceived, andFatallifecycle events.Error - The property, which holds the scope that the component is connected to, or
Scopewhen the component isn't connected.null - The method, which fetches the current server state and applies it to the synchronized properties.
Hydrate
Scope resolution
When runs, the client bindings component resolves its scope and connects automatically. How the component resolves its scope depends on the CloudBehaviour's :
EnableClient()[StateScope]- scope: The client bindings component follows whichever player is signed in. To follow one specific player instead, open the Inspector window and set Target Player ID, or call
PlayerinEnableClient(playerId). The component then connects only while that player is signed in, so a later account switch doesn't redirect calls to another player's state. To find the player ID, refer to Get the player ID.OnEnable - scope: The client bindings component follows the single active session, after the player signs in and joins the session. If your project runs more than one session at a time, choose which session to follow. In the Inspector window, set Target Session Type, or call
MultiplayerSessioninstead ofEnableClient(sessionType). Otherwise, the client bindings component raisesEnableClient().Fatal
Before the scope resolves, the client bindings component isn't connected, and an RPC call can't reach the CloudBehaviour. Trigger RPC calls from player input or from the event, rather than immediately after .
ScopeChangedEnableClient()Lifecycle events
The client bindings component raises lifecycle events to report changes to its connection and state. The following table describes each lifecycle event.
Event | Description |
|---|---|
| Raised when the scope changes or is withdrawn, such as when the player signs out. When a scope resolves, the synchronized properties are already up to date. Check that |
| Raised after the component applies the server state to the synchronized properties. This event doesn't indicate which properties changed. |
| Raised whenever the component receives a message. The synchronized properties are already up to date when this event is raised. |
| Raised when the component encounters a failure from which it can't recover. The component is already disabled when this event is raised. To reconnect, call |
| Raised when a failure occurs that doesn't stop the component. The component keeps its scope. To find out what failed and whether to retry, read the |
State updates
The client bindings component applies server state to its synchronized properties in the following situations:
- An RPC call returns.
- A push message arrives, either a synchronized state update or a push channel event.
- The scope resolves.
- A call completes.
Hydrate
Push messages reach only connected clients. To catch up on state that changed while the component wasn't connected, call .
HydrateThe Inspector window of the client bindings component shows the connection status and the current values of the synchronized state.