文档

View and manage state

View, update, and clear the stored state of a player or multiplayer session from the Unity Dashboard or the Cloud Code Admin API.
阅读时间3 分钟最后更新于 3 天前

Cloud Code stores state for each scope, whether that's a player or a multiplayer session. You can view and change a scope's state outside your game, for example to check a player's progress or to reset a session that no longer progresses.

Manage state in the Unity Dashboard

To find a scope's state in the Unity Dashboard, follow these steps:
  1. In the Unity Dashboard, select Products.
  2. Select Cloud Code.
  3. Select C# Modules > State inspector.
  4. Select an environment.
  5. Select Multiplayer session or Player.
  6. Select a session ID or player ID, or enter the ID and select Fetch.
After you select or fetch a scope, the State inspector displays the scope's module state, with a row for each class, and its active timers.
To edit a class's state, follow these steps:
  1. In the Module state section, expand the class's row to view its current state as JSON.
  2. Select the pencil icon on the class's row.
  3. Edit the JSON, then save your changes.
After you save your changes, the Module state section displays the class's updated state.
In the Module state section, select Clear state to clear all of the scope's module state. In the Timers section, select Clear timers to clear all of the scope's active timers.

Manage state with the Cloud Code Admin API

Use the Cloud Code Admin API to add the same workflow to your own tools. The API has an endpoint to get, update, and clear a scope's state.
To manage a scope's state with the Admin API, follow these steps:
  1. Authenticate with a service account that has the permission for the operation you want to perform:
    • Get module state:
      cloud_code.modules.get
    • Update module state:
      cloud_code.modules.update
    • Clear module state:
      cloud_code.modules.delete
  2. Identify the scope type (
    player
    or
    multiplayerSession
    ) and the player ID or session ID.
  3. Call one of the following operations:
    • Get module state returns the state of each class as JSON, plus any active timers.
    • Update module state replaces the whole state of each class you send. It only changes the modules in the request, and you can't use it to change timers.
    • Clear module state removes the stored state, the active timers, or both.
After you call an operation, Get module state and Update module state return the scope's current state and its
ETag
value, and Clear module state returns a
204
response. If an operation returns a
404
error, the scope has no stored state yet. Cloud Code saves a module's state about a second after the module first runs for a player or session.

Update limits

You can update a scope only if it already has stored state, so you can't use the API to create state for a scope that has never run.
You can't update the state of the following classes. Get module state lists them under
clearOnly
instead of
modules
, and you can only clear their state:
  • Classes that use a custom
    IStateSerializer
    . For details, refer to State serialization.
  • Classes removed from the deployed module.
  • State that Cloud Code doesn't store as JSON.
To make sure an update doesn't overwrite changes made since you last read the state, send the
ETag
value from Get module state in the
If-Match
header of your update. If the state changed in the meantime, the update returns a
412
error and nothing changes.

Additional resources