Storage¶
The storage module allows to store data persistently and retrieve values at a later time. This data is stored in the context of your project. Attaching the same script to a different project will mean reading and writing to a different store. Furthermore the storage is local on your machine. This means it is great for savegames, but not so great for storing a highscore table that other players should also see.
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 tovalue (
any
) – the value to write
- 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¶
local Storage = require 'engine/storage'
local Console = require 'engine/console'
local Controller = Class.new(Entity)
function Controller:init()
local value = "Hello World"
Storage.set("greeting", value)
Storage.get("greeting"):next(function(value)
Console.log(value)
end)
end
return Controller
local Storage = require 'engine/storage'
local Tools = require 'engine/tools'
local Controller = Class.new(Entity)
function Controller:init()
Storage.keys():next(function(keys)
Tools.printDump(keys)
end)
end
return Controller