Sub-tables & key values conversion

I don’t know if they’re technically called sub tables ( a table within a table ) but it seems like an appropriate name to me.

Here’s what I can’t seem to get to work, if you convert a table with a sub table to key values and then back again, any attempt to get information from the sub table returns nil. However if I used PrintTable, it displays all the sub table information correctly.

[lua]
table_test[“a”] = { “one”, “two”, “three” } – add the sub table
print( table_test[“a”][1] ) – should print “one”
print( table_test[“a”][2] ) – should print “two”
print( table_test[“a”][3] ) – should print “three”

> PrintTable( table_test )…
a:
1 = one
2 = two
3 = three

converted_table = util.TableToKeyValues( table_test ) – convert to key values
table_test2 = util.KeyValuesToTable( converted_table ) – convert back

print( table_test2[“a”][1] ) – should print “one”
print( table_test2[“a”][2] ) – should print “two”
print( table_test2[“a”][3] ) – should print “three”
– but all three of these return nil, even though table_test2 should be identical to table_test.

> PrintTable( table_test2 )…
a:
1 = one
3 = three
2 = two

[/lua]

Just thought I’d put this here to see if if there’s a better way of handling sub-tables.

Don’t use TableToKeyValues unless you absolutely have to. GLON is a preferable alternative.

[lua]local t = {}
t[“a”] = { “one”, “two”, “three” }

local t_encode = glon.encode(t);
local t_decode = glon.decode(t_encode)

PrintTable(t_decode)[/lua]

Also, your problem is that KeyValuesToTable translates all keys into strings, I believe. So it should work if you do

[lua]print( table_test2[“a”][“1”] ) – should print “one”
print( table_test2[“a”][“2”] ) – should print “two”
print( table_test2[“a”][“3”] ) – should print “three” [/lua]

Thanks, and when using GLON would I still have to sanitise the table to use vectors/angles?

GLON can encode most everything but functions, including vectors, angles, and entities. Read more here: http://wiki.garrysmod.com/?title=GLON

Does it still panic over NPCs and vehicles?