Page 1 of 2

Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 4:31 pm
by pancakepalace
Hi guys and girls,

I was having difficulty testing my apps without being able to inspect the contents of a table by printing it on the screen like var_dump does in PHP. I pulled my sleeves, poured a cup of coffee, and coded this little snippet.

If you have any suggestions for improvements, that would be most appreciated.

All lua types are supported except functions and userData. If someone knows how to support functions by writing them out as strings, that would be great. I'm not even sure if it's possible.

The main function is below. You can include it in your code however you like. Make it a local function if you can.

Code: Select all

--[[
	Dumps a table for inspection

	@param  table   table to inspect
	@param  mixed   table name [def: 1]
	@param  number  x starting position [def: 10]
	@param  number  y starting position [def: 10]
	@param  number  tab width [def: 20]
	@param  number  newLine height [def: 20]
	@param  mixed   number of newLines at EOL [def: 2]
	@return numbers x ending position, y ending position
--]]
function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
	k = k or 1
	x = x or 10
	y = y or 10
	tab = tab or 20
	NL = NL or 20
	if numNLatEOF == nil then numNLatEOF = 2 end
	if type(k) == "number" then k = "[" .. k .. "]" end
	love.graphics.print(k .. " = {", x, y)
	x = x + tab
	y = y + NL
	for k,v in pairs(t) do
		if type(v) == "table" then
			x, y = dumpTable(v, k, x, y, tab, NL, false)
		else
			if v == true then v = "true"
				elseif v == false then v = "false"
				elseif type(v) == "string" then v = '"' .. v .. '"'
				elseif type(v) == "function" then v = "function"
				elseif type(v) == "userdata" then v = "userdata"
			end
			if type(k) == "number" then k = "[" .. k .. "]" end
			love.graphics.print(k .. ' = ' .. v, x, y)
		end
		y = y + NL
	end
	x = x - tab
	love.graphics.print("}", x, y)
	if numNLatEOF then y = y + NL * numNLatEOF end
	return x, y
end
You can call the function inside love draw. Here are some pseudo code examples. There are no examples showing the modification of x, y, tab, or newLine, but I'm sure you can figure that out.

Code: Select all

-- Ex: Dumping one table
function love.draw()
	dumpTable(table)
end

-- Ex: Dumping one table with a name
function love.draw()
	dumpTable(table, "tableName")
end

-- Ex: Dumping many tables
function love.draw()
	local x, y = dumpTable(table, "tableName")
	x, y = dumpTable(table, "tableName", x, y)
	x, y = dumpTable(table, "tableName", x, y)
	dumpTable(table, "tableName", x, y)
end
I hope that helps somebody. I'm sure finding it useful.

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 4:45 pm
by bartbes
pancakepalace wrote:If someone knows how to support functions by writing them out as strings, that would be great.
string.dump

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 5:12 pm
by pancakepalace
Unfortunately string.dump changes the function into binary code which is not really human readable. What would have been great would have been to change this:

Code: Select all

ThisIsAFunction = function(x) return x end
into this:

Code: Select all

ThisIsAHumanReadableString = "function(x) return x end"
NOTE: Iv'e modified my snippet to display 'function' and 'userdata' when either type is encountered. The version I had previously posted reported an error for those types.

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 5:19 pm
by Robin
pancakepalace wrote:Unfortunately string.dump changes the function into binary code which is not really human readable.
Well, if you want to do that, you need to include a disassembler. Unfortunately, there are not that many for Lua 5.1, and they output human readable Lua byte code, and not Lua.

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 5:21 pm
by pancakepalace
Thanks for the heads up Robin. This is just a quick function for debugging, so I won't go that far! I'd rather spend time making games to debug than diving in to a full fledge debugger!

NOTE: Corrected a bug in the snippet in case you downloaded it early than this post.

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 6:26 pm
by slime
Robin wrote:
pancakepalace wrote:Unfortunately string.dump changes the function into binary code which is not really human readable.
Well, if you want to do that, you need to include a disassembler. Unfortunately, there are not that many for Lua 5.1, and they output human readable Lua byte code, and not Lua.
http://luadec51.luaforge.net/

http://forum.xda-developers.com/showthread.php?t=568281

http://forum.xda-developers.com/showpos ... stcount=49

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 6:37 pm
by Robin
I am aware of LuaDec51, but it seemed like it couldn't do much more than I described earlier. If this is not the case, please tell me. :)
I have not seen this, but aa bunch of exes hardly seems appropriate for a LÖVE projects. ;)

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 6:43 pm
by slime
Robin wrote:
I am aware of LuaDec51, but it seemed like it couldn't do much more than I described earlier. If this is not the case, please tell me. :)
I have not seen this, but aa bunch of exes hardly seems appropriate for a LÖVE projects. ;)
You'll get ~90% complete code by using one of those (depending on if the code uses certain aspects of Lua or not).

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 7:18 pm
by pancakepalace
That sure seems like overkill to add a simple feature to a very simple function used only in debugging.

Looking at my function, I'm wondering if I really need those 'local' keywords at the beginning to make the variables local. I remember reading that function arguments were always local, except for tables that get overwritten. This is an aspect of Lua that always confuses me. The problem is I saw many code examples using the 'local' keyword in this instance when setting default values for the function arguments. Somehow though, I think this is superfluous. Just not sure.

Code: Select all

function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
	local k = k or 1
	local x = x or 10
	local y = y or 10
	local tab = tab or 20
	local NL = NL or 20
etc...
end

Re: Snippet for dumping tables when testing

Posted: Wed Sep 21, 2011 8:45 pm
by kraftman
pancakepalace wrote:That sure seems like overkill to add a simple feature to a very simple function used only in debugging.

Looking at my function, I'm wondering if I really need those 'local' keywords at the beginning to make the variables local. I remember reading that function arguments were always local, except for tables that get overwritten. This is an aspect of Lua that always confuses me. The problem is I saw many code examples using the 'local' keyword in this instance when setting default values for the function arguments. Somehow though, I think this is superfluous. Just not sure.

Code: Select all

function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
	local k = k or 1
	local x = x or 10
	local y = y or 10
	local tab = tab or 20
	local NL = NL or 20
etc...
end
yeh local here is redundant.