Forums » Bugs
lua::print_r and object properties
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?
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?
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
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
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
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
I wanted one that would recurse through just incase =) but thank you that's handy too..
Why is this in bugs tho?