Documentation

Cloud Code

Open Unity Dashboard

Cloud Code

Access control in CloudBehaviours

Understand the access levels that control which callers can call a CloudBehaviour endpoint, when to use each one, and how access levels behave with synchronized state and host migration.
Read time 5 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.
Every CloudBehaviour endpoint has an access level. By default, Cloud Code resolves the access level from the class's
[StateScope]
. A
Scope.Player
class defaults to
Access.Player
, and a
Scope.MultiplayerSession
class defaults to
Access.SessionMember
.
You can override the default access level for each endpoint, choose the level that fits each endpoint, protect a player's data from other players, and keep host-only endpoints working when the session's host changes.

Override the default access level

Add
[CloudCodeAccess]
only where a specific endpoint needs to differ from the scope's default. The following example keeps the default on one endpoint and overrides it on two others.
[StateScope(Scope.MultiplayerSession)]public class MatchState : CloudBehaviour{ // This endpoint uses the default access level, Access.SessionMember. public void SubmitMove(string move) { } // Only the session host can call this endpoint. [CloudCodeAccess(Access.SessionHost)] public void StartMatch() { } // Only non-player callers, such as a timer, can call this endpoint. [CloudCodeAccess(Access.Service)] public void EndTurn(int turnNumber) { }}

Access levels

The following table lists each
Access
value, the scopes it's valid on, and who can call an endpoint that uses it:

Value

Valid on

Who can call

Unspecified
Any scopeResolves from the class scope at load time (the default):
Access.Player
for
Scope.Player
,
Access.SessionMember
for
Scope.MultiplayerSession
Global
Any scopeAny authenticated caller
Service
Any scopeNon-player callers only, such as other CloudBehaviours, service accounts, or trusted backends, never a player
Player
Scope.Player
only
The player who owns the scope, and any non-player caller
SessionMember
Scope.MultiplayerSession
only
Any member of the session, and any non-player caller
SessionHost
Scope.MultiplayerSession
only
The host of the session, and any non-player caller
If you set an
Access
value that the class's scope doesn't support, the module throws an exception when it loads.

Choose an access level

Use
Access.Service
on an endpoint that only a non-player caller has a reason to call, such as a timer callback, or an endpoint that another CloudBehaviour calls to update a player's record. If logic only runs as part of another endpoint, such as granting gold when a player makes a move, make that logic a private method instead. A private method isn't an endpoint, so no client can call the method. Use
Access.SessionHost
for match flow control, such as starting or force-advancing a match, where one client is authoritative. In other cases, the scope's default access level is usually correct.
Important
Access control restricts who can call an endpoint, but not who receives the changes the endpoint makes. When any endpoint changes synchronized state, the new state reaches every client in the scope. Every endpoint's response also carries the scope's synchronized state to the caller, whether or not the caller is in the scope. To keep data private, use a private field, or refer to Per-player state views in CloudBehaviours.
For example, the client bindings don't include
Access.Service
endpoints, so a client can't call those endpoints, but every client still receives the synchronized state that those endpoints change.

Protect player data from other players

A player can bind a client only to their own
Scope.Player
state, but any player can call an endpoint directly with another player's scope. The default
Access.Player
level refuses those calls. An
Access.Global
endpoint accepts those calls. On a
Scope.Player
CloudBehaviour, an
Access.Global
endpoint lets any authenticated player do the following in another player's scope:
  • Read that player's whole synchronized state, which the endpoint's response returns, even when the endpoint itself returns nothing.
  • Change that player's state through whatever the endpoint does.
Use
Access.Global
on a
Scope.Player
CloudBehaviour only when the endpoint is safe for any player to call. To keep data that only the owner sees out of other players' responses, store the data in a private field marked
[CloudCodeSerializeProperty]
so that the data persists between calls. Then expose the field through a
[SyncView]
method that returns the value only to the owner. Cloud Code builds each response for the calling player. On a
Scope.Player
CloudBehaviour,
Context.ScopeId
is the owner's player ID.
The following example lets any player gift gold to another player, but only the owner can read their balance.
[StateScope(Scope.Player)]public class Wallet : CloudBehaviour{ [CloudCodeSerializeProperty] private int m_Gold; // Only the owner sees their gold. Other callers receive null. [SyncView("Gold")] public int? Gold(string playerId) => playerId == Context.ScopeId ? m_Gold : null; // Any player can gift gold, but can't read the balance. [CloudCodeAccess(Access.Global)] public void Gift(int amount) => m_Gold += amount;}
A
[SyncView]
method controls only what each player sees. The method doesn't stop an
Access.Global
endpoint from changing the owner's state, and doesn't hide the value that the endpoint returns. For more information, refer to Per-player state views in CloudBehaviours.

Keep host-only endpoints working after host migration

When the session host leaves, the session moves the host role to another member.
Access.SessionHost
checks the session's current host on every call, so host-only endpoints keep working for the new host and reject the previous host.
Use
Access.SessionHost
for host-only endpoints instead of comparing
Context.PlayerId
with a host ID that the CloudBehaviour stores in a field. A stored field doesn't change when the session's host changes. After the host leaves, the check rejects the new host, so no player can call the endpoint.
Inside an endpoint,
Context.Session.HostId
holds the session's current host.
Context.Session.PlayerIds
lists the session's members in no particular order, so the first player ID isn't the host.

Keep a synchronized host ID up to date

If your game stores the host in a synchronized field, for example to show which player is the host, the field doesn't change when the host leaves. Each time the session's host changes, Multiplayer Services emits a
lobby-host-changed
trigger event. Use this event to call an
Access.Service
endpoint in the session's scope that updates the field. Cloud Code then synchronizes the new host ID to every client in the session.

Additional resources