Monocle - Love2D debugging for 0.9.0

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
20047m
Prole
Posts: 13
Joined: Sun Nov 03, 2013 5:44 am

Monocle - Love2D debugging for 0.9.0

Post 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 282 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 ;)
Last edited by 20047m on Thu Jan 30, 2014 1:38 am, edited 4 times in total.
User avatar
JovialFeline
Prole
Posts: 28
Joined: Wed Jan 08, 2014 2:32 pm

Re: Monocle - Love2D debugging for 0.9.0

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Monocle - Love2D debugging for 0.9.0

Post 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.
Help us help you: attach a .love.
User avatar
20047m
Prole
Posts: 13
Joined: Sun Nov 03, 2013 5:44 am

Re: Monocle - Love2D debugging for 0.9.0

Post 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:
Post Reply

Who is online

Users browsing this forum: No registered users and 211 guests