lua-savestate
Persist some keys and values
Usage
1. Install
Setup lua-loader and then just npm install lua-savestate
2. Require
local savestate = require("lua-savestate")
3. Init
Comes with one packaged implementation, for the Corona SDK, which is also the default, but you still have to initialise it:
savestate:init()
You can provide some default values:
savestate:init({ foo = "bar", bar = "baz"})
And of course you can provide your own implementation, which would be the second argument to init
:
savestate:init(nil, { path = "/full/path/to/the/file.ext", serialise = function (object) return convert_table_with_keys_and_values_to_a_string(object) end, deserialise = function (text) return convert_string_to_a_table_with_keys_and_values(text) end})
If you have a custom implementation in a separate lua file, you can pass the module load string for it, eg: savestate:init(nil, "coronasdk")
(which happens to be equivalent to savestate:init()
)
4. Read & write
To read a value:
savestate:get("foo")
To create or update a value:
savestate:set("foo", "foobar")
To save all values:
savestate:persist()
To get all values saved when setting a value:
savestate:set("foo", "foobar", true)
To get all known values:
for _,key in savestate:keys() do print(key, savestate:get(key))end
Limitations
- Currently only file-based persistence. API must break to support others.