Forums » Bugs

lua::print_r and object properties

May 02, 2009 Palamedes link
I'm still new to lua and am goofing off with a plugin..

If I do "local item = GetStationMerchInfo(10)" how do I find out what all the possible properties for item are?

The API doesn't appear to be documented very well .. item.name, item.price, item.type I figured out but I'm sure there are more..

In PHP you can do a print_r on a datatype to get all manner of useful information.. is there something similar in lua?
May 02, 2009 Palamedes link
Never mind this seems to work nicely;

function print_r(t, indent, done)
done = done or {}
indent = indent or 0
if type(t) == "table" then
for key, value in pairs (t) do
console_print(string.rep(" ", indent)) -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
console_print(string.format("[%s] => table", tostring (key)));
console_print(string.rep(" ", indent+4)) -- indent it
console_print("(");
itemdump.print_r(value, indent +7, done)
console_print(string.rep(" ", indent+4)) -- indent it
console_print(")");
else
console_print(string.format("[%s] => %s", tostring (key), tostring (value)))
end
end
else
console_print(t)
end
end
May 02, 2009 mr_spuck link
The game comes with the printtable function. It's not recursive though and not as fancy.

I think it's just something like

function printtable(t)
 for k,v in pairs(t) do console_print(tostring(k)..": "..tostring(v)) end
end
May 02, 2009 Palamedes link
I wanted one that would recurse through just incase =) but thank you that's handy too..
May 03, 2009 maq link
Why is this in bugs tho?