Cross-behaviour calls in CloudBehaviours
Call another CloudBehaviour's endpoint from inside your own, including across different scopes.
Read time 3 minutesLast updated 15 days ago
Cloud Code generates an interface for every CloudBehaviour in your module. Inject the interface with , then call a member on it to get back an unexecuted . Run the with to invoke another instance of that CloudBehaviour right now. An does nothing until you run it. If you build one and never call or on it, the build fails with .
I{Behaviour}[CloudCodeService]ICallICallForScope(scopeId)ICallForScopeScheduleAsyncCCSG133Call another CloudBehaviour with ForScope
The following example shows a -scoped updating each player's at the end of a match. A is -scoped because it belongs to one player and outlives any single match:
MultiplayerSessionMatchRoomPlayerRecordPlayerRecordPlayer[StateScope(Scope.Player)]public class PlayerRecord : CloudBehaviour{ public int MatchesPlayed; public int PersonalBest; [CloudCodeAccess(Access.Service)] public int RecordMatch(int score) { MatchesPlayed++; if (score > PersonalBest) PersonalBest = score; return PersonalBest; }}[StateScope(Scope.MultiplayerSession)]public class MatchRoom : CloudBehaviour{ [CloudCodeSerializeProperty] private Dictionary<string, int> _scores = new(); public async Task EndMatch([CloudCodeService] IPlayerRecord playerRecord) { foreach (var entry in _scores) { var best = await playerRecord.RecordMatch(entry.Value).ForScope(entry.Key); Logger.LogInformation("{PlayerId} personal best is now {Best}", entry.Key, best); } }}
ForScope[StateScope]entry.KeyPlayerRecordScope.PlayerGet a scope ID
Read the scope ID to pass to from the CloudBehaviour's :
ForScopeContextTarget scope | Scope ID | Notes |
|---|---|---|
| | The ID of the player making the current call. For the other players in a session, use |
| | Only available when the calling CloudBehaviour is |
Context.PlayerIdnullLock the target down
A cross-behaviour call arrives with no player identity. The player ID you pass to only selects which scope instance runs the endpoint. It doesn't make the call on that player's behalf.
The target is still an endpoint that a client can call directly. Mark the target so no player can call it directly. Refer to Access control in CloudBehaviours.
ForScope[CloudCodeAccess(Access.Service)]Requirements
A cross-behaviour call has the following requirements:
- Both CloudBehaviours must live in the same module. If the target is in another module, call it as a plain Cloud Code module instead.
- Mark the injected parameter with . Refer to Inject Cloud Code services.
[CloudCodeService] - Register the target's interface in your module's . In this example, call
ICloudCodeSetup. Refer to Register the generated interface.config.AddPlayerRecordClient() - Name only the endpoint's real arguments. The generator strips every parameter from
[CloudCodeService], so there's no slot to pass a value into.I{Behaviour} - Class names must be unique within the module, even across namespaces.