• Serializing Tables - Recursive
    2 replies, posted
Hey, I'm currently having a problem with my serialization script that should store a table, and a table of items finally, each item is a table that contains information. function I'm using to serialize my tables. (It is basic I know, however, I just need something basic for this task.) [code] function SerializeTable(tTable, bIsRec) local sString = "\n{" if bIsRec then sString = "" end for index, value in pairs(tTable) do if type(value) == "number" then sString = sString .. index .. " = " .. value .. "," elseif type(value) == "string" then sString = sString .. index .. " = '" .. value .. "'," elseif type(value) == "table" then local newString = SerializeTable(value, true) if type(index) == "number" then sString = sString .. "\n t".. index .. " = { " .. newString .. "," else sString = sString .. index .. " = { " .. newString .. "," end end end sString = sString .. "}" return sString end [/code] This usually outputs a file like this [code] local tInventory = {iSlots = 12,tItems = { t1 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t2 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t3 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t4 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t5 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t6 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t7 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t8 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t9 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,}, t10 = { sName = 'Base Item',iCurStack = 3,ID = 1,iMaxStack = 3,},},iSpeedPen = 0,iCurItems = 10,sName = 'Small Backpack',} [/code] Now to load the .txt file I use this function [code] function LoadInventory(ply) -- Remove : from the SteamID and run the file saved local steamID = string.gsub(string.sub(ply:SteamID(),7), ":", "_") local tInven = file.Read(INVEN_DIR..steamID..".txt") if tInven == nil then return nil end -- tInventory is defined when tInven is run CompileString(tInven, "Test") PrintTable(tInventory) return tInventory end [/code] This works for the most part HOWEVER, it will not return the tables within tItems and returns nothing at all. The confusing part is, if I do copy and paste this file into a lua file, it will run perfectly.
Where is tInventory set to anything? Shouldn't it be tInventory = CompileString()? Also you don't seem to be serializing anything that current systems can't, you could save tables as JSON or Valves file format. [url=http://wiki.garrysmod.com/page/Global/CompileString]CompileString[/url] [url=http://wiki.garrysmod.com/page/util/KeyValuesToTable]util.KeyValuesToTable[/url] [url=http://wiki.garrysmod.com/page/util/TableToKeyValues]util.TableToKeyValues[/url] [url=http://wiki.garrysmod.com/page/util/TableToJSON]util.TableToJSON[/url] [url=http://wiki.garrysmod.com/page/util/JSONToTable]util.JSONToTable[/url]
sorry, I do declare the variable earlier on in the serialization proccess. I'll try out JSON this afternoon, didnt know there was a readily available alternative to GLON.
Sorry, you need to Log In to post a reply to this thread.