SaveSlot

A versioned save in the device registry. What goes in it is up to the game: write() any JSON-friendly value and read() it back. It's stored as {version, data}.

read() only returns a save that can be used: one written with this version, or an older one migrate() upgrades, that passes the validate function if one's given. Anything else reads as invalid, so "Continue" can be shown with exists(validate).

Set enabled = false to stop it reading or writing (e.g. while a deep link is testing a level). Subclass and override migrate() to upgrade old saves.

Properties

  • section (string)
  • key (string)
  • version (integer)
  • enabled (boolean) — While false, read() returns invalid and write()/delete() do nothing.

Example

CODE
slot = new BGE.SaveSlot("my-game", "save", 2)
slot.write({level: 3, coins: 40})
save = slot.read(validateMySave) ' function(data) as dynamic: data, or invalid if bad

Constructor

new SaveSlot( section: string, key?: string, version?: integer, ): SaveSlot

Parameters

  • section (string) — registry section, e.g. your game's id
  • key (string, optional, default: "\"save\"")
  • version (integer, optional, default: 1) — bump when the save's shape changes

Instance Methods

read(validate?: function): dynamic

The saved data, upgraded and validated, or invalid if there's none it can use.

Parameters

  • validate (function, optional, default: "invalid") — function(data) as dynamic returning the (possibly cleaned up) data, or invalid to reject it

Returns

  • dynamic

write(data: dynamic): boolean

Saves data under the current version.

Parameters

  • data (dynamic) — anything FormatJson() can write

Returns

  • boolean — false if the slot is disabled or the data couldn't be written (see BGE.registryWrite())

delete(): boolean

Deletes the save.

Returns

  • boolean — false if the slot is disabled

exists(validate?: function): boolean

Is there a save read() would return?

Parameters

  • validate (function, optional, default: "invalid") — as for read()

Returns

  • boolean

migrate(fromVersion: integer, data: dynamic): dynamic

Upgrades a save from an older version. Return the upgraded data, or invalid to drop it. The default drops every older save, so override this before bumping version or players lose their progress. fromVersion is 0 for anything SaveSlot didn't write (e.g. a save from before the game used SaveSlot) - recognise your own old format there. An upgraded save is written back, so this runs once per save.

Parameters

  • fromVersion (integer)
  • data (dynamic)

Returns

  • dynamic