[code] Debugging your Lua scripts

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

[code] Debugging your Lua scripts

Post by Durante »

I suck, so I often need lots of debugging information.
So I made "debug.lua":

Code: Select all

white = Color.new(255,255,255)
lightgray = Color.new(200,200,200)

Debug = {
	argcache = {}
}
function Debug:print(...)
	table.insert(self.argcache, arg)
	self:printNumbered(table.getn(self.argcache))
end
function Debug:printNumbered(i)
	arg = self.argcache[i]
	screen:clear()
	screen:print(10, 10, "Debug call:", lightgray)
	for i,v in ipairs(arg) do
		screen:print(10, 10+10*i, tostring(v), white)
		if type(v) == "table" and table.getn(v) == 2 then -- it's a point, probably
			screen:print(200, 10+10*i, "{ " .. tostring(v[1]) .. ", " .. tostring(v[2]) .. " }", white)
		end
	end
	screen:print(10, 250, "(press start to continue, select to save as \"debug"..i..".png\")", lightgray)
	screen:print(10, 260, "(debug message "..i.." of "..
		table.getn(self.argcache)..", use L and R to navigate)", lightgray)
	screen.flip()
	
	System.sleep(200)
	local n = nil
	repeat
		ctrl = Controls.read()
		if ctrl:start() then break
		elseif ctrl:select() then screen:save("debug"..i..".png")
		elseif ctrl:l() and i > 1 then n = i-1
		elseif ctrl&#58;r&#40;&#41; and i < table.getn&#40;self.argcache&#41; then n = i+1
		end
	until n
	if n then self&#58;printNumbered&#40;n&#41; end
end
You use it by calling Debug:print(what, ever, arguments, you, need).
The result should look somewhat like this:
Image

Hope this helps some of you who produce as many bugs as I do!

(Now, what would be really cool is combining this with some input method and eval'ing the code right then, so you'd be able to inspect & alter your program state on the fly ;) )
Last edited by Durante on Sun Oct 30, 2005 3:13 am, edited 1 time in total.
chaos
Posts: 135
Joined: Sun Apr 10, 2005 5:05 pm

Post by chaos »

that looks pretty useful..
Chaosmachine Studios: High Quality Homebrew.
Post Reply