Sorry, we don't support your browser.  Install a modern browser

Add a flat string->string dictionary for storing data on an object#811

There was a discussion on the discord about storage of data on objects. The conversation led to the idea of a userdata option (called storage or data?) that is structured like a table (string keys and values only) and Benjamin Dobell asked for a nolt suggestion to be opened so he can remember.

3 years ago

the specific interface would be something like

object.Data["myThing"] = "myValue"
object.Data.myThing = "myValue"
local myThing = object.Data.myThing
local data = object.Data -- Returns a table (copy)

Currently there is only on script_state field, so if you just want one value from its script_state you have to decode the whole thing. this would let us keep small pieces of data in separate easily accessible fields.

3 years ago

Do you want more lags when an object comes from a bag or deck? Do you want to get huge table from bag.getObjects()?

It’s better to use global script that does the trick:

--Global
local DICT = {} 
DICT[object.guid] = {} --new dictionary
DICT[object.guid]["myThing"] = "myValue"

function onSave()
    return JSON.encode(DICT)
end

function onLoad(script_state)
    DICT = JSON.decode(script_state)
end
3 years ago

that approach is even worse than storing data in individual script_states on objects. you have one huge json for the entire mod that needs to be decoded to read anything. Decoding json in lua is slow and should be done as little as possible, which was part of the reasoning for the original suggestion.

3 years ago