dot big bang

Developer API
Menu

Class SaveData<ReadDataType, WriteDataType>

All
  • Public
  • Public/Protected
  • All

An object that lets you persist data for the current player across game sessions.

You can use it to keep track of unlocks, progression, etc. It serves the same purpose as a save file, but it is on the dotbigbang servers.

When you call .write(data), the data you provide will be saved on dotbigbang's servers, associated with the current player. If that player opens the game again later, you can get the same data back by calling .read().

The data itself can be any JSON-serializable value. For example:

let data = { checkpoint: "forest", unlockedHats: [1, 6, 3], equippedHat: 6 };

Data is saved to the server asynchronously, but .read() returns optimistic data synchronously. To accomplish this, a local data cache is used. For more information on this cache, see SaveData.read.

Type parameters

  • ReadDataType

  • WriteDataType: ReadDataType

Hierarchy

  • SaveData

Index

Accessors

Methods

Accessors

status

Methods

read

  • read(): null | ReadDataType
  • Get the JSON-parsed contents of the latest save for the current user.

    If the current user has never saved before, this method returns null.

    Note that calls to SaveData.read are optimistic; that is, if you call .write with some data, calling .read will return that same data even if it hasn't been synced yet. To do this, a local cache of what we think is on the server (or soon will be on the server) is utilized. In most cases, this is sufficient. However, if your game may be played on a spotty network connection, knowing whether data actually made it to the server can be important. To get accurate info about whether save data has been persisted on the server, leverage SaveData.status, UserScriptComponent.onSaveDataSynced and/or UserScriptComponent.onSaveDataSyncFailed.

    Returns null | ReadDataType

setCachedData

  • setCachedData(data: WriteDataType): void
  • Manually override the local cache of data as optimistically returned by SaveData.read without sending a request to the server to write that data. This method should be used cautiously, as it can get your local representation of the user's save data out of sync with what is on the server. However, you may wish to use it in UserScriptComponent.onSaveDataSyncFailed to reset the local cache of the data back to the last known synced data, which you can obtain via SaveData.status.

    Parameters

    • data: WriteDataType

      The data to put into the cache.

    Returns void

write

  • write(data: WriteDataType): number
  • Save the provided data to the server on behalf of the current user. When .read() is called later on behalf of this user, the same data will be returned.

    Parameters

    • data: WriteDataType

      The data to save. Must be JSON-serializable.

    Returns number

    A write id "ticket", to be used to keep track of in-flight save requests to the server. See UserScriptComponent.onSaveDataSynced and UserScriptComponent.onSaveDataSyncFailed for more info.

    Note that calls to SaveData.read are optimistic; that is, if you call .write with some data, calling .read will return that same data even if it hasn't been synced yet. To do this, a local cache of what we think is on the server (or soon will be on the server) is utilized. In most cases, this is sufficient. However, if your game may be played on a spotty network connection, knowing whether data actually made it to the server can be important. To get accurate info about whether save data has been persisted on the server, leverage SaveData.status, UserScriptComponent.onSaveDataSynced and/or UserScriptComponent.onSaveDataSyncFailed.