jetlag2d
    Preparing search index...

    Class StorageDevice

    StorageDevice provides three key/value stores

    • Persistent: Things saved using the HTML5 persistent storage API. You should try to only save serialized JSON here. Ideally, you'll load it at game start into the Session, and then flush the whole thing back when there is something worth saving.
    • Session: Things that last as the player moves among levels, but are discarded when the player exits/refreshes the browser.
    • Level: Things that last only until the player moves to another level.
    Index
    • Look up a SessionValue's value. SessionValue.get() calls this; user code shouldn't need to. The first time, the value is made with initial.

      Type Parameters

      • T

      Parameters

      • key: SessionValue<T>

        The SessionValue

      • initial: () => T

        The function that makes the value

      Returns T

    • Load saved data from persistent storage. Persistent storage can only hold plain data: numbers, strings, booleans, arrays, and plain objects made of those.

      The recommended way to use it is to load once, at the start of the game, into a SessionValue, and then work with the session value:

      class Progress { best = 0; unlocked = 1; }
      const PROGRESS = new SessionValue("progress", () => stage.storage.loadPersistent("progress", new Progress()));
      

      defaults is used when nothing has been saved. When something has been saved, each field that defaults has is replaced by the saved value, but only if the saved value is the same kind of thing (a number for a number, and so on). Anything else that was saved is ignored. That means that old or damaged saves can't put the wrong kind of thing in your fields, and a field that you add in a later version simply gets its default.

      Type Parameters

      • T extends object

      Parameters

      • key: string

        The name that the data was saved under

      • defaults: T

        An object with every field that you want, set to what it should be if nothing was saved

      Returns T

      defaults, with the saved values filled in

    • Save plain data to persistent storage, to be there the next time the game is played.

      It only saves plain data. If the data holds a function, a Map, a Set, or anything else that can't be saved, this throws.

      Parameters

      • key: string

        The name to save the data under

      • data: object

        The object to save

      Returns void