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 7 minutesLast updated 2 days ago
The client bindings are a generated component that connects your game to a CloudBehaviour. The client bindings 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 following example shows the script, which 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 client bindings component is connected to, or
Scopewhen it isn't connected.null - The method, which fetches the current server state and applies it to the synchronized properties.
Hydrate - The and
EnableClientmethods, which enable and disable the client bindings component. The component must be enabled to synchronize the CloudBehaviour's state and to call its RPCs. TheDisableClientproperty indicates whether the component is enabled.IsClientEnabled
Control when client bindings synchronize
The client bindings component synchronizes with the CloudBehaviour only while it's enabled. The client bindings script calls in and in , so by default the client bindings component synchronizes automatically whenever it's enabled and its GameObject is active.
EnableClient()OnEnableDisableClient()OnDisableThe following table describes what each method does.
Method | Description |
|---|---|
| Starts tracking the scope. When the scope resolves, the client bindings component fetches the current server state, applies it to the synchronized properties, subscribes to push messages, and raises |
| Stops tracking the scope, closes the push message subscription, and withdraws the scope. If the client bindings component had a scope, it raises |
IsClientEnabledtrueEnableClientScopenullTo synchronize at a different time, replace the call in with your own call. For example, the following class starts to synchronize only when your game calls , and stops when the client bindings component is disabled.
EnableClient()OnEnableStartSynchronizing[CloudBehaviour(typeof(MyCloudBehaviour))]public partial class MyCloudBehaviourClient : MonoBehaviour{ public void StartSynchronizing() => EnableClient(); protected virtual void OnDisable() => DisableClient();}
Because a second call has no effect while the client bindings component is enabled, don't call from your own code while also calls it. To change the target of an enabled client bindings component, call first.
EnableClientEnableClientOnEnableDisableClientRe-enable with the same overload
EnableClientEnableClient()EnableClient()The following table lists what a later call follows after each overload.
EnableClient()Overload | Scope | Target that the overload sets | What a later |
|---|---|---|---|
| | Target Player ID is set to the player ID. | The same player. |
| | Target Session Type is set to the session type. | Whichever session of that type is current. |
| | Target Session Type is set to the observer's session type. | Whichever session of that type is current, through a new observer instead of the one you passed. |
| | Target Session Type is set to the session's type. | Whichever session of that type is current, not specifically the session you passed. |
| | Target Session Type is set to the asset's session type. | Whichever session of that type is current, no longer tied to the asset's session. |
To keep the same target after or a event, re-enable the client bindings component with the same overload that you first enabled it with. In the following example, always passes the same asset, so the client bindings component follows that asset's session each time it's re-enabled.
DisableClientFatalOnEnableMultiplayerSession[CloudBehaviour(typeof(MyCloudBehaviour))]public partial class MyCloudBehaviourClient : MonoBehaviour{ [SerializeField] MultiplayerSession m_Session; protected virtual void OnEnable() => EnableClient(m_Session); protected virtual void OnDisable() => DisableClient();}
Scope resolution
When runs, the client bindings component resolves its scope and connects automatically. How the client bindings 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)instead ofOnEnable. The client bindings 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.EnableClient() - 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(). You can also pass a session object instead of a session type. For the available overloads, refer to Re-enable with the same overload.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 client bindings component applies the server state to the synchronized properties. This event doesn't indicate which properties changed. |
| Raised whenever the client bindings component receives a message. The synchronized properties are already up to date when this event is raised. |
| Raised when the client bindings component encounters a failure from which it can't recover. It's already disabled when this event is raised. To reconnect, call |
| Raised when a failure occurs that doesn't stop the client bindings component. The client bindings 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 client bindings 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.