Ryan F. Mercer
Whys
Minor edit. By allowing serialize() to also handle nil as an empty string, I can now itemize the nil value, which does not store the value, but rather removes both the key and value from the table for complete deletion. This appears to be automatic for Lua.
Empty strings are still handled normally.
Test:
Output:
Empty strings are still handled normally.
Code:
function serialize( data )
local r = "";
local sub = function( v )
[COLOR="Blue"]if v == nil then
v = "";
end[/COLOR]
if v == false or v == true or type( v ) == "number" then
v = tostring( v );
else
v = v:gsub('"', "\[QUOTE\]");
v = v:gsub('{', "\[LCB\]");
v = v:gsub('}', "\[RCB\]");
v = "\""..v.."\""; -- string
end
return v;
end
if type( data ) ~= "table" then
r = sub( data );
else
r = "{ ";
local num = 0;
for k,v in pairs( data ) do
if num > 0 then
r = r.." ";
end
r = r..k.."=";
if type( v ) == "table" then
r = r..serialize( v );
else
r = r..sub( v );
end
num = num +1;
end
r = r.." }";
end
return r;
end
Test:
Code:
include( "SaveUtils" );
function testThis()
local pPlayer = GetPlayer();
save( "myMod", pPlayer, "key1", "test" );
save( "myMod", pPlayer, "key1", nil );
save( "myMod", pPlayer, "key2", "" );
print( "test1: "..serialize(load( "myMod", pPlayer, "key1" )) );
print( "test2: "..serialize(load( nil, pPlayer )) );
end
Events.ActivePlayerTurnStart.Add( testThis );
Output:
Code:
test1: ""
test2: { myMod={ key2="" } }