Page 1 of 1

Monocle - Love2D debugging for 0.9.0

Posted: Sun Jan 26, 2014 2:01 am
by 20047m
Hey Lövers. I've been looking through the various debug libraries that the Love2D community has to offer, of which some of them are great, some are okay, and some don't even work anymore in version 0.9.0. I couldn't quite find what I was looking for, though, so I decided to create my own library. I present, Monocle. Because monocles are awesome. I took some of the ideas from the libraries that were currently out there (lick, donut, etc) and put them together. With Monocle, you can watch variables as you play. In addition, you optionally edit the game, and Monocle will watch files and reload ‘main.lua’ automatically when it spots something has changed.
monocle.lua
v0.2.0. see https://github.com/kurtjarvi/monocle/releases for the most current version, along with correct documentation.
(5.92 KiB) Downloaded 297 times
v0.2.0
Current Features
  1. Can watch variables and complex expressions
  2. Can watch files and reload them when they change
  3. Custom error handler that will display the values of all of your watched variables in the error screen
  4. Reloads the game after any files have been changed, even after an error has occured
  5. Custom colors
Planned Features
  1. In-game console for debugging purposes
  2. Ability to use the aforementioned in-game console *even* AFTER the game has crashed
Easy to implement, easy to use, and easy to customize. I know there are other debug libraries like this, but I wanted to make something that people could easily setup and use in 0.9.0 with very little hassle and effort. It’s not done yet, though, so check back often to watch things as they update. The goal is to make a debug library that's easy to use, versatile, and is feature packed for ease of use.

Implementation:

Code: Select all

require ‘monocle’
Monocle.new({})

function love.update(dt)
    Monocle.update()
end
function love.draw()
    Monocle.draw()
end
function love.textinput(t)
    Monocle.textinput(t)
end
function love.keypressed(text)
    Monocle.keypressed(text)
end
And that’s it. Let’s say we want to watch the current FPS of the game. We just need to add this line somewhere in our code:

Code: Select all

Monocle.watch("FPS",  function() return math.floor(1/love.timer.getDelta()) end)
Image
And voilà, when you press the debug button (by default it’s the “~” key, but you can set it yourself), the watched variable will show up in the corner, along with the name you gave it!

Oh, you can watch string and number variables too (tables soon). so if you have a variable player.health that you really want to watch,

Code: Select all

Monocle.watch("health", function() return player.health end)
is perfectly valid.

The best part? Even if there's an error, the values of your expressions at the time of the error still show up.
Image

Also, you can pass in certain parameters when you load Monocle. Here's a list of them, with their default values:

Code: Select all

Monocle.new({       -- ALL of these parameters are optional!
	isActive=true,          -- Whether the debugger is initially active
	customPrinter=false,    -- Whether Monocle prints status messages to the output
	printColor = {51,51,51},-- Color to print with
	debugToggle='`',        -- The keyboard button for toggling Monocle
	filesToWatch=           -- Files that, when edited, cause the game to reload automatically
		{
			'main.lua'
		}
})
Here's the repository, for all you GitHub fans.
https://github.com/kurtjarvi/monocle



I hope y'all enjoy ;)

Re: Monocle - Love2D debugging for 0.9.0

Posted: Sun Jan 26, 2014 6:07 am
by JovialFeline
Groovy. Now I can harness the power of dual Monocles. :monocle:

I may well use this once I've got a few things in order, so kudos for your work.

Re: Monocle - Love2D debugging for 0.9.0

Posted: Sun Jan 26, 2014 9:33 am
by Robin
I'd change one thing: do not use loadstring. Instead, use functions:

Code: Select all

Monocle.watch("FPS", function() math.floor(1/love.timer.getDelta()) end)
(And then remove the loadstring in monocle.lua.)

It's better for many reasons, one among them is that it's faster to call a function than to parse it, compile it, and then call it. Also, every time you eval a string literal, your soul dies a little.

Re: Monocle - Love2D debugging for 0.9.0

Posted: Sun Jan 26, 2014 10:05 pm
by 20047m
Robin wrote:

Code: Select all

Monocle.watch("FPS", function() math.floor(1/love.timer.getDelta()) end)
(And then remove the loadstring in monocle.lua.)
Done! Thank you, Robin! That has been implemented in this newest version, which you can grab from the top of the page or at https://github.com/kurtjarvi/monocle/releases. Thanks for the input, I really appreciate it! You forgot a return statement though ;)

:megagrin: