Essentially to have ease of usability of tables with relatively non-volatile script data.
In plugins I have MUSH variables named after characters, that contain the serialized table of the data this plugin looks after. The currently used character name is located in another plugin.
Basically, I would like to be able to use something like,
and have the MUSH variable be updated after each operation. So changes persist reloading of scripting, and script variables do not need to be re-initialised when reloading happens.
Attempting to modify
Prepended 'local' so the table will be discarded after the function returns a copy of it.
Sort of stuck on the syntax to return a reference to this table. Is it possible? I suspect assert and loadstring are not the way to go... And also how to serialize back in. Hmm.
Haven't really tested properly because of this, if there are any glaringly obvious logic errors, please point out.
Any suggestions?
In plugins I have MUSH variables named after characters, that contain the serialized table of the data this plugin looks after. The currently used character name is located in another plugin.
Basically, I would like to be able to use something like,
char = GetPluginVariable(active_character_pID,"character")
...
v.char.option.omit.that = true
...
if not v.char.option.omit.that then -- carry on printing
...
v.char.stats.defends.dodge = v.char.stats.defends.dodge + 1and have the MUSH variable be updated after each operation. So changes persist reloading of scripting, and script variables do not need to be re-initialised when reloading happens.
Attempting to modify
v={} -- variables table
setmetatable(v,{
-- called to access an entry
__index=
function(t,name)
-- check to see if MUSH variable contains serialized table
if string.find(GetVariable(name),"^"..name.." = {}"))
-- loading into local scope
assert(loadstring("local "..GetVariable(name)))()
-- return copy of this table, how?
-- otherwise is a regular MUSH variable
else
return GetVariable(name)
end
end;
-- called to change or delete an entry
__newindex=
function(t,name,val)
local result
if val==nil then
result=DeleteVariable(name)
-- elseif type(val)=="table" then
-- SetVariable(name,serialize(name)) -- this doesn't seem right?
else
result=SetVariable(name,tostring(val))
end
-- warn if they are using bad variable names
if result==error_code.eInvalidObjectLabel then
error("Bad variable name '"..name.."'")
end
end;
})Prepended 'local' so the table will be discarded after the function returns a copy of it.
Sort of stuck on the syntax to return a reference to this table. Is it possible? I suspect assert and loadstring are not the way to go... And also how to serialize back in. Hmm.
Haven't really tested properly because of this, if there are any glaringly obvious logic errors, please point out.
Any suggestions?