Storage

The storage module allows to store data persistently and retrieve values at a later time. This data is stored in the context of the root item of your project. So usually in the context of a scene. Attaching the same script to a different scene will mean reading and writing to a different store.

Reference

storage.get(key)

Retrieve a value from storage. The function will not directly return the value but the value is fetched from storage asynchronously and an event is generated once the value is available. See examples.

Parameters:

key (str) – the key to retrieve

storage.set(key, value)

Set a value in storage

Parameters:
  • key (str) – the key to write to

  • value (any) – the value to write

storage.remove(key)

Remove a value from storage

Parameters:

key (str) – the key to remove

storage.clear()

Clear the storage

storage.keys()

Retrieve all keys from storage. The function will not directly return the values but the data is fetched from storage asynchronously and an event is generated once the data is available. See examples.

Examples

Set and retrieve value
local Storage = require 'engine/storage'
local Console = require 'engine/console'

function init()
    local value = "Hello World"
    Storage.set("greeting", value)
    Storage.get("greeting"):next(function(value)
        Console.log(value)
    end)
end
Retrieve all keys from storage
local Storage = require 'engine/storage'
local Console = require 'engine/console'
local Tools = require 'engine/tools'

function init()
    Storage.keys():next(function(keys)
        Console.log(Tools.dump(keys))
    end)
end