Here are a serialization and unserialization function I created for my own use. It will store a variable as basic XML.
Examples of use (Lua):
this_is_an_array = {}
world.SetVariable('VarName',serialize(this_is_an_array))
this_is_an_array = unserialize(world.GetVariable()) or {}
--SERIALIZATION FUNCTIONS
function serialize(var,compression)
if(type(var) == 'table') then
local output = ''
for index,value in pairs(var) do
if(not (value == var)) then
output = output .. '<i i="' .. index .. '">' .. serialize(value) .. '</i>'
end
end
if(compression) then
return '<ct>' .. utils.base64encode(utils.compress(output,9)) .. '</ct>'
else
return '<t>' .. output .. '</t>'
end
elseif(type(var) == 'number') then
return '<n>' .. var .. '</n>'
elseif(type(var) == 'string') then
return '<s>' .. var .. '</s>'
elseif(type(var) == 'boolean') then
return '<b>' .. (var and 't' or 'f') .. '</b>'
end
return ''
end
function unserialize(var)
if(var == nil) then
return nil
end
local root = utils.xmlread(var)
if(root) then
return unserialize_xml(root.nodes[1])
else
return nil
end
end
function unserialize_xml(node)
if(node.name == 'n') then return tonumber(node.content) end
if(node.name == 's') then return tostring(node.content) end
if(node.name == 'b') then return (tostring(node.content) == 't') end
if(node.name == 'ct') then
node.name = 't'
local root = utils.xmlread(utils.decompress(utils.base64decode(node.content)))
if(root) then
node.nodes = root.nodes
else
return {}
end
end
if(node.name == 't') then
local t = {}
if(node.nodes) then
for _,child in ipairs(node.nodes) do
if(child.name == 'i') then
t[child.attributes['i']] = unserialize_xml(child.nodes[1])
end
end
end
return t
end
return nil
end
I would recommend putting the functions in a seperate script file which you can include through the world scripting interface. Then your triggers can call these functions as they execute their own code. Hope that helps. -Tsunami