melonJS
    Preparing search index...

    Interface SaveAPI

    The shape of the save namespace.

    Declared rather than left as a bare index signature: typed as Record<string, unknown> every member was swallowed by the index, so save.add came out as unknown and could not be CALLED at all under strict — the namespace's own documented example did not typecheck — while every registered key read back as unknown and needed a cast to compare or assign.

    The index signature stays, so anything registered elsewhere is still reachable; declared members simply take precedence over it.

    interface SaveAPI {
        add<T extends Record<string, Jsonifiable>>(props: T): SaveAPI & T;
        createCloudStorage(options: CloudStorageOptions): CloudStorage;
        remove(key: string): void;
        setStorage(storage: SaveStorage): void;
        [key: string]: unknown;
    }

    Indexable

    • [key: string]: unknown

      keys registered through SaveAPI.add

    Index
    • Add new keys to the storage (localStorage by default) and set them to the given default values if they do not exist.

      Type Parameters

      • T extends Record<string, Jsonifiable>

      Parameters

      • props: T

        key and corresponding values

      Returns SaveAPI & T

      this namespace, typed with the keys just registered, so they can be read back without a cast

      // Initialize "score" and "lives" with default values
      me.save.add({ score : 0, lives : 3 });
      // get or set the value through me.save
      me.save.score = 1000;
      // or keep the returned view, and read the keys with their real types
      const store = me.save.add({ score : 0, lives : 3 });
      store.lives -= 1;
    • Make a storage for setStorage() out of a platform's cloud save, such as YouTube Playables' or Facebook Instant Games': the save is loaded once, before the game starts, and everything is saved back in one piece.

      • data is what the platform loaded: an object, or that object in JSON, or nothing the first time. The storage starts from it and keeps every item in memory, so save reads and writes without waiting.
      • write(items) saves every item back to the platform. Changes made together are saved in one call, and calls are made one at a time: changes made while one is in progress go in the next one, with the latest values. A platform that refuses a save while another is pending, as Facebook's does, never sees two.
      • A write that fails (its promise rejects) is logged, and the items are saved again with the next change, or by flush().
      • flush() saves at once and resolves when everything is saved: call it when the platform pauses or closes the game.

      Parameters

      Returns CloudStorage

      the storage, to hand to setStorage()

      // YouTube Playables
      const cloud = me.save.createCloudStorage({
      data: await ytgame.game.loadData(),
      write: (items) => ytgame.game.saveData(JSON.stringify(items)),
      });
      me.save.setStorage(cloud);
      ytgame.system.onPause(() => cloud.flush());
      // Facebook Instant Games
      const { save: data } = await FBInstant.player.getDataAsync(["save"]);
      me.save.setStorage(me.save.createCloudStorage({
      data,
      write: (items) => FBInstant.player.setDataAsync({ save: items }),
      }));
    • Remove a key from the storage. The key stays registered: setting it again saves it again, and it is restored on the next load like any other.

      Parameters

      • key: string

        key to be removed

      Returns void

      // Remove the "score" key from localStorage
      me.save.remove("score");
    • Keep the saved keys somewhere other than the browser's localStorage, the default: a game portal's cloud save, for example, or the storage of a native wrapper. storage is anything with the getItem, setItem and removeItem methods of the Web Storage API (see SaveStorage), which save calls as it calls localStorage's, so save.setStorage(localStorage) goes back to the default.

      • The keys the new storage holds are read at once, and replace the values read from the previous one. A key registered with add() that it does not hold keeps its value, and is written to it. So it can be called before or after add().
      • The calls are synchronous, as localStorage's are. For a cloud save, which loads and saves asynchronously, use createCloudStorage(): it holds the items in memory once they are loaded, and saves in the background.
      • Nothing is copied to the previous storage, nor removed from it.

      Parameters

      • storage: SaveStorage

        where the keys are kept from now on

      Returns void

      // a platform's cloud save: load it first, then hand it to save
      const data = await portal.loadData();
      me.save.setStorage(me.save.createCloudStorage({
      data,
      write: (items) => portal.saveData(JSON.stringify(items)),
      }));
      // the value in the cloud save, or 0 the first time
      me.save.add({ hiscore : 0 });
      // a storage of your own, such as a native wrapper's: three synchronous calls
      me.save.setStorage({
      getItem: (key) => native.read(key) ?? null,
      setItem: (key, value) => native.write(key, value),
      removeItem: (key) => native.erase(key),
      });
      // back to the browser's localStorage
      me.save.setStorage(localStorage);