After seeing a friend's code for returning tables from mushclient variables while emulating var.lua, I saw an improvement in their code that could be made.
This allows one to use the mushvars table to directly edit the MUSHclient variables without the need to type out SetVariable(), and automatically serializes and unserializes tables to and from MUSHclient variables so they can be read as if they were normal tables. (basically, the built in var.lua with table functionality). If it can be tonumber()ed, it will also return it in number form instead of a string.
Usage: Just treat it like a normal lua table.
mushvars.test = "test"
mushvars.test1 = 1
mushvars.testtab = {"whathaveyou", i = "am a named variable", {"i am in a table"}}
testvars.test = nil --deletes the variable
require "serialize"
function mushvartable(table)
return setmetatable({}, {
__index = function (table, key)
if not GetVariable(key) then
return nil
elseif not tonumber(GetVariable(key)) then
if string.match(GetVariable(key), "{") and string.match(GetVariable(key), "}") then
local retp = GetVariable(key)
loadstring("local ret = "..retp)()
return ret
else
return GetVariable(key)
end
else
return tonumber(key)
end
end,
__newindex = function(table, key, value)
if type(value) == 'nil' then
DeleteVariable(key)
elseif type(value) == 'table' then
SetVariable(key, serialize.save_simple(value))
else
SetVariable(key, value)
end
end,
__metatable = false
});
end
mushvars = mushvartable {}
This allows one to use the mushvars table to directly edit the MUSHclient variables without the need to type out SetVariable(), and automatically serializes and unserializes tables to and from MUSHclient variables so they can be read as if they were normal tables. (basically, the built in var.lua with table functionality). If it can be tonumber()ed, it will also return it in number form instead of a string.
Usage: Just treat it like a normal lua table.
mushvars.test = "test"
mushvars.test1 = 1
mushvars.testtab = {"whathaveyou", i = "am a named variable", {"i am in a table"}}
testvars.test = nil --deletes the variable