I want to create a function to use to initialize variable tables that will serialize themselves whenever something is changes within them. The only way I could think of doing this is with the metafunction __newindex. But since it seems to call itself I am getting a stack overflow error and it halts. Anyone know how do go about doing this?
require "serialize"
function VarInit(name)
-- Declare and load the table
_G[name] = {}
if GetVariable(name) then loadstring(GetVariable(name))() end
-- Set the metatable to save itself
setmetatable(_G[name], {
__index = function(t, k)
return t['_' .. k]
end,
__newindex = function(t, k, v)
_G[name]['_' .. k] = v
SetVariable(name, serialize.save(name))
end
})
end