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
Every CloudBehaviour endpoint has an access level. By default, Cloud Code resolves the access level from the class's . A class defaults to , and a class defaults to .
[StateScope]Scope.PlayerAccess.PlayerScope.MultiplayerSessionAccess.SessionMemberYou 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 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.
[CloudCodeAccess][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 value, the scopes it's valid on, and who can call an endpoint that uses it:
AccessValue | Valid on | Who can call |
|---|---|---|
| Any scope | Resolves from the class scope at load time (the default): |
| Any scope | Any authenticated caller |
| Any scope | Non-player callers only, such as other CloudBehaviours, service accounts, or trusted backends, never a player |
| | The player who owns the scope, and any non-player caller |
| | Any member of the session, and any non-player caller |
| | The host of the session, and any non-player caller |
If you set an value that the class's scope doesn't support, the module throws an exception when it loads.
AccessChoose an access level
Use 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 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.
Access.ServiceAccess.SessionHostFor example, the client bindings don't include endpoints, so a client can't call those endpoints, but every client still receives the synchronized state that those endpoints change.
Access.ServiceProtect player data from other players
A player can bind a client only to their own state, but any player can call an endpoint directly with another player's scope. The default level refuses those calls. An endpoint accepts those calls. On a CloudBehaviour, an endpoint lets any authenticated player do the following in another player's scope:
Scope.PlayerAccess.PlayerAccess.GlobalScope.PlayerAccess.Global- 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 on a 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 so that the data persists between calls. Then expose the field through a method that returns the value only to the owner. Cloud Code builds each response for the calling player. On a CloudBehaviour, is the owner's player ID.
Access.GlobalScope.Player[CloudCodeSerializeProperty][SyncView]Scope.PlayerContext.ScopeIdThe 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 method controls only what each player sees. The method doesn't stop an 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.
[SyncView]Access.GlobalKeep host-only endpoints working after host migration
When the session host leaves, the session moves the host role to another member. checks the session's current host on every call, so host-only endpoints keep working for the new host and reject the previous host.
Access.SessionHostUse for host-only endpoints instead of comparing 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.
Access.SessionHostContext.PlayerIdInside an endpoint, holds the session's current host. lists the session's members in no particular order, so the first player ID isn't the host.
Context.Session.HostIdContext.Session.PlayerIdsKeep 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 trigger event. Use this event to call an endpoint in the session's scope that updates the field. Cloud Code then synchronizes the new host ID to every client in the session.
lobby-host-changedAccess.Service