Untitled

📰Getting Started | **Updates |** 📝 Guides | 🔢 API | ❓FAQ

Guides Overview

The Storage API allows you cache persistent data across various scopes, including device, experience, and in the cloud. The interface behaves similarly to the current web standards.

Any data saved in the web editor will be removed when the session ends.

const storage = require('storage-preview');

Scopes

Here are the currently available scopes:

storage.local // per-experience
storage.device // per-device
storage.player // per-player cloud storage
storage.players // access all players

Accessing Data

For each scope, data can be saved and retrieved using the same functions setItem and getItem. Complex objects can be used with JSON.stringify and JSON.parse. Data can be removed from storage via removeItem and clear. Clear will only remove data for the currently loaded experience.

var data = {
  points: 26,
  currency: 42
}

// store data
storage.local.setItem('session', JSON.stringify(data));

// retrieve data.
var data = JSON.parse(storage.local.getItem('session'));

if (data) {
  log.info('Previous data found.');
  log.info('  Points: ' + data.points);
  log.info('  Currency: ' + data.currency);
}

Keys

Information about keys can be accessed via length and key. This can be useful to iterate over storage data.

const numKeys = storage.local.length;

for (var i = 0; i < numKeys; i++) {
const key = storage.local.key(i);
const data = JSON.parse(storage.local.getItem(key));
  log.info(key + ': ' + data);
}

Module Properties

local <Storage>

The storage object for the current device.

player <Storage>

storage.player supports, loading, modifying, and persisting data to the cloud for the current player. The following APIs are provided in addition to the Storage API

storage.player.loadPlayer(userid)

Makes available the cloud data for the given user in storage.player . Returns an IAsyncJsResponse that has then() and fail() methods that are called back accordingly when the operation is completed

storage.player.persistPlayer()

Persists the current player to the cloud. Returns an IAsyncJsResponse that has then() and fail() methods that are called back accordingly when the operation is completed.

players

storage.players supports reading a different players data from the cloud. This data is not cached on devices and all APIs return an IAsyncJsResponse (the standard Storage API is not supported for this reason)

storage.players.getPlayerItem(userid, key)

Returns the string value associated with the key for the given player in an IAsyncJsResponse

storage.players.getPlayerData(userid)

Returns all of the data associated with the given user as a JSON dictionary

Storage Class Properties

length <int>

The number of entries stored.

Storage Class Methods

key(index)

Fetches a storage key based on an index.

getItem(key)

Gets a value based on a key and returns null if not found.

setItem(key, value)

Sets a value for a given key.

removeItem(key)

Removes an entry from storage for a given key.

clear()

Clears all data from storage.

Next: System

Sidebar Table of Contents


Untitled

Copyright © 2021 Enklu, Inc.