Player and match persistence in CloudBehaviours
Understand what Cloud Code persists for a CloudBehaviour between invocations, and how to control it.
Read time 3 minutesLast updated 2 days ago
Synchronization and persistence are independent. Synchronization controls whether a member reaches a client, and persistence controls whether a member survives between invocations. A member can synchronize, persist, do both, or do neither.
Control which members persist
By default, a public member persists in the cloud between invocations, and a non-public member doesn't. Two attributes override that default, and both attributes apply to fields and properties:
Attribute | Effect |
|---|---|
| Persists the member. Use it to persist a non-public member. |
| Keeps the member out of persistence. Use it to stop a public member from persisting. |
Neither attribute changes whether a member synchronizes. A public member synchronizes and a non-public member doesn't, whichever attribute you apply.
Persistence applies member by member inside a persisted object too. A private field in a persisted custom type needs its own .
[CloudCodeSerializeProperty]The following example applies both attributes:
// This field survives invocations, but Cloud Code never sends it to a client.[CloudCodeSerializeProperty]private string _correctAnswer;// This field synchronizes for display, but Cloud Code doesn't persist it.[CloudCodeIgnoreProperty]public int FramesSinceStart;
Player scope compared with session scope
Scope.PlayerScope.PlayerScope.MultiplayerSessionScope.MultiplayerSessionScopes and their lifetimes come from the underlying Stateful Cloud Code module. For the full serialization mechanics, refer to State serialization in Cloud Code.
Common persistence mistakes
Cloud Code saves only serialized members. A plain private field without can keep its value temporarily. However, the field resets whenever Cloud Code reloads your CloudBehaviour from saved state, which can happen between any two calls. If a value unexpectedly resets, confirm that the member is public or marked with .
[CloudCodeSerializeProperty][CloudCodeSerializeProperty]Cloud Code doesn't persist a computed property, such as . A get-only property persists only when Cloud Code can refill it in place, such as a get-only collection. For any other get-only property, give the property a setter so Cloud Code can restore it. A private setter is enough. To exclude a property from persistence, use .
public int Total => A + B;[CloudCodeIgnoreProperty]Cloud Code saves the state of a CloudBehaviour under its module name and class name. Moving a CloudBehaviour to another namespace in the same module keeps its state. However, if you rename a CloudBehaviour or its module, the next call starts with a new instance that has none of the saved state. Cloud Code keeps the old state, but doesn't load it again unless you change the name back. The State inspector and the Cloud Code Admin API list the old state as and only let you clear it. For more information, refer to View and manage state.
<ClassName>_System