IMIsle Modkit
Pages in this area

storage

Server

modkit.storage is a key/value store. Every resource has its own store, saved as data\storage\<resource>.json under the server root. The file is loaded when the resource starts.

Writes are collected. Two seconds after the first unsaved change the whole store is written on a background thread, through a temp file and a rename. A crash inside those two seconds loses the last changes. Call save() after a change that must not get lost.

Values can be booleans, numbers, strings and tables of those, up to 16 levels deep. A table with entries at 1..n is stored as a list, every other table as an object with string keys. Functions are refused.

get returns a copy. Changing the returned table does not change the store, call set again.

modkit.storage.get

modkit.storage.get(key [, default])

Returns: the stored value, or default when the key does not exist.

local visits = modkit.storage.get("visits", 0)

modkit.storage.set

modkit.storage.set(key, value)

Parameter Type Description
key string
value any nil removes the key
modkit.events.add("playerConnected", function(player)
  local seen = modkit.storage.get("seen", {})
  seen[player.steamId] = os.time()
  modkit.storage.set("seen", seen)
end)

modkit.storage.delete

modkit.storage.delete(key)

Returns: true when the key existed.

modkit.storage.keys, all

modkit.storage.keys() returns an array of all keys. modkit.storage.all() returns a copy of the whole store as a table.

modkit.storage.save

modkit.storage.save()

Hands the store to the writer thread now instead of after the two second wait.