2
votes

I followed this tutorial in Corona SDK documentation.

I am trying to print all the entries and subtables, and the entries in those subtables from a display object.

In Corona SDK, a display object is a Lua table and so I tried the basic things, as listed in the tutorial.

for k,v in pairs(myTable) do
    print( k,v )
end

There is also a fancier function that should output all subtables, namely:

local function printTable( t )

    local printTable_cache = {}

    local function sub_printTable( t, indent )

        if ( printTable_cache[tostring(t)] ) then
            print( indent .. "*" .. tostring(t) )
        else
            printTable_cache[tostring(t)] = true
            if ( type( t ) == "table" ) then
                for pos,val in pairs( t ) do
                    if ( type(val) == "table" ) then
                        print( indent .. "[" .. pos .. "] => " .. tostring( t ).. " {" )
                        sub_printTable( val, indent .. string.rep( " ", string.len(pos)+8 ) )
                        print( indent .. string.rep( " ", string.len(pos)+6 ) .. "}" )
                    elseif ( type(val) == "string" ) then
                        print( indent .. "[" .. pos .. '] => "' .. val .. '"' )
                    else
                        print( indent .. "[" .. pos .. "] => " .. tostring(val) )
                    end
                end
            else
                print( indent..tostring(t) )
            end
        end
    end

    if ( type(t) == "table" ) then
        print( tostring(t) .. " {" )
        sub_printTable( t, "  " )
        print( "}" )
    else
        sub_printTable( t, "  " )
    end
end

But neither of these actually print out all the entries in these tables. If I create a simple rectangle and try to use either of the functions, I only get two tables but I know that there's more there:

local myRectangle = display.newRect( 0, 0, 120, 40 )
for k,v in pairs(myRectangle) do
    print( k,v ) -- prints out "_class table, _proxy userdata"
end
print( myRectangle.x, myRectangle.width ) -- but this prints out "0, 120"

-- But if I were to add something to the table, then the loop finds that as well, e.g.

local myRectangle = display.newRect( 0, 0, 120, 40 )
myRectangle.secret = "hello"
for k,v in pairs(myRectangle) do
    print( k,v ) -- prints out "_class table, _proxy userdata, secret hello"
end

So, how can I print out everything included in a display object? Clearly these two approaches don't get the entries in the main display object table.

2

2 Answers

0
votes

In general you can't, as you may have a metatable associated with that table that will generate results for fields "on the fly". In this case you get back something like a ShapeObject that provides methods like fill, path, setFillColor and others, but these methods are hidden behind a userdata object. It also provides inheritance, so there are some fields you can query that "belong" to other classes. You can use pairs/ipairs to get the list of keys in the table, which is what you get in your example (and setting secret adds a new key to the table).

0
votes

Regarding properties (not methods) of display object - display objects in Corona have special property displayObject._properties Documentation

_properties will return string, so you have two approaches of printing it:

local myRectangle = display.newRect( 0, 0, 120, 40 )

-- Approach 1:
-- Prettify here to make better readability to json
local json = require("json")
print("myRectangle._properties: ", json.prettify( myRectangle._properties ) )

-- Approach 2:
local json = require("json")
local myRectangleProps = json.decode(myRectangle._properties) -- I skipped error handling here, because there shouldn't be any
for k,v in pairs(myRectangleProps) do
    print( k,v ) -- prints x 0, width 120, etc
end