Documentation

Cloud Code

Open Unity Dashboard

Cloud Code

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

Important
This page describes an experimental feature that might change significantly before release. It's not recommended to use or rely on experimental features in production environments due to potential instability.
The client bindings are a generated
MonoBehaviour
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.
For 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
Client
folder. The following example shows the script, which is a partial
MonoBehaviour
class marked with the
[CloudBehaviour]
attribute.
[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
    UnityEvent
    for each push channel that the cloud class declares.
  • The
    ScopeChanged
    ,
    Synchronized
    ,
    MessageReceived
    ,
    Fatal
    , and
    Error
    lifecycle events.
  • The
    Scope
    property, which holds the scope that the client bindings component is connected to, or
    null
    when it isn't connected.
  • The
    Hydrate
    method, which fetches the current server state and applies it to the synchronized properties.
  • The
    EnableClient
    and
    DisableClient
    methods, which enable and disable the client bindings component. The component must be enabled to synchronize the CloudBehaviour's state and to call its RPCs. The
    IsClientEnabled
    property indicates whether the component is enabled.
Important
If your cloud script has a
CCSG
error, the Editor doesn't generate your client bindings, so it never compiles the client assembly. None of these members exist until you fix the error. The Console window shows the
CCSG
error on your cloud script. Fix the error first.

Control when client bindings synchronize

The client bindings component synchronizes with the CloudBehaviour only while it's enabled. The client bindings script calls
EnableClient()
in
OnEnable
and
DisableClient()
in
OnDisable
, so by default the client bindings component synchronizes automatically whenever it's enabled and its GameObject is active.
The following table describes what each method does.

Method

Description

EnableClient
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
ScopeChanged
. If the client bindings component is already enabled,
EnableClient
logs a warning and does nothing.
DisableClient
Stops tracking the scope, closes the push message subscription, and withdraws the scope. If the client bindings component had a scope, it raises
ScopeChanged
and
Scope
becomes
null
. The synchronized properties keep their last values but stop receiving updates. If the client bindings component isn't enabled,
DisableClient
does nothing.
IsClientEnabled
is
true
from the moment
EnableClient
runs, even before the scope resolves. The client bindings component is connected only when
Scope
isn't
null
.
To synchronize at a different time, replace the
EnableClient()
call in
OnEnable
with your own call. For example, the following class starts to synchronize only when your game calls
StartSynchronizing
, and stops when the client bindings component is disabled.
[CloudBehaviour(typeof(MyCloudBehaviour))]public partial class MyCloudBehaviourClient : MonoBehaviour{ public void StartSynchronizing() => EnableClient(); protected virtual void OnDisable() => DisableClient();}
Because a second
EnableClient
call has no effect while the client bindings component is enabled, don't call
EnableClient
from your own code while
OnEnable
also calls it. To change the target of an enabled client bindings component, call
DisableClient
first.

Re-enable with the same overload

EnableClient
has several overloads. Every overload except
EnableClient()
replaces the target that's set in the Inspector window (Target Player ID or Target Session Type) with the target that you pass. A later
EnableClient()
call uses that replaced target, which might not be the same session that you first passed.
The following table lists what a later
EnableClient()
call follows after each overload.

Overload

Scope

Target that the overload sets

What a later
EnableClient()
call follows

EnableClient(string playerId)
Player
Target Player ID is set to the player ID.The same player.
EnableClient(string sessionType)
MultiplayerSession
Target Session Type is set to the session type.Whichever session of that type is current.
EnableClient(SessionObserver observer)
MultiplayerSession
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.
EnableClient(ISession session)
MultiplayerSession
Target Session Type is set to the session's type.Whichever session of that type is current, not specifically the session you passed.
EnableClient(MultiplayerSession session)
MultiplayerSession
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
DisableClient
or a
Fatal
event, re-enable the client bindings component with the same overload that you first enabled it with. In the following example,
OnEnable
always passes the same
MultiplayerSession
asset, so the client bindings component follows that asset's session each time it's re-enabled.
[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
EnableClient()
runs, the client bindings component resolves its scope and connects automatically. How the client bindings component resolves its scope depends on the CloudBehaviour's
[StateScope]
:
  • Player
    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
    EnableClient(playerId)
    in
    OnEnable
    instead of
    EnableClient()
    . 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.
  • MultiplayerSession
    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
    EnableClient(sessionType)
    instead of
    EnableClient()
    . Otherwise, the client bindings component raises
    Fatal
    . You can also pass a session object instead of a session type. For the available overloads, refer to Re-enable with the same overload.
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
ScopeChanged
event, rather than immediately after
EnableClient()
.

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

ScopeChanged
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
Scope
isn't
null
before you call an RPC.
Synchronized
Raised after the client bindings component applies the server state to the synchronized properties. This event doesn't indicate which properties changed.
MessageReceived
Raised whenever the client bindings component receives a message. The synchronized properties are already up to date when this event is raised.
Fatal
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
EnableClient
again with the same overload that you first enabled it with, but don't call it from the
Fatal
handler.
Error
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
Reason
property of the
CloudBehaviourException
argument.

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
    Hydrate
    call completes.
Push messages reach only connected clients. To catch up on state that changed while the client bindings component wasn't connected, call
Hydrate
.
The Inspector window of the client bindings component shows the connection status and the current values of the synchronized state.

Additional resources