Page 1 of 1

Patman's Console Library

Posted: Sun May 19, 2013 9:06 pm
by Patman
Hello LÖVE peoples.

I've developed a really simple console library that you can use in pretty much anything. It's inspired by WoW's console and is toggled with the ~ key (though you can change it easily).

console.lua: http://paste2.org/zYhxXOXD

Usage:

1. Copy console.lua to your project's folder.
2. Tweak the options at the top to whatever tickles your fancy.
3. Add require "console" to the top of main.lua.
4. Whenever you want to log something, simply use console.log(msg) where msg is anything (it's automatically converted to a string so don't worry about non-string types).
5. If you don't want to have to call console.load()/.draw()/.keypressed()/.update() then simply call console.stealth() at the bottom of main and it will automatically hook those into your program.

Example 1 (hello world):

Code: Select all

require "console"

function love.load()
	console.log("Hello world from the console!")
end

console.stealth()
Example 2 (counter):

Code: Select all

require "console"

local timer = 0
local counter = 1

function love.update(dt)
	if timer < 1 then
		timer = timer + dt
		return
	else
		timer = 0
	end

	console.log(counter)
	counter = counter + 1
end

function love.keypressed(key)
	if key == "tab" then
		console.load()
		counter = 1
	end
end

function love.draw()
	love.graphics.setColor(255, 50, 50)
	love.graphics.rectangle("fill", 30, 30, 740, 540)
	love.graphics.setColor(0, 0, 0)
	love.graphics.print("Press TAB to reset the console!", 40, 540)
end

console.stealth()
Example 3 (hello world with no stealth):

Code: Select all

require "console"

function love.load()
	console.load()
	console.log("Hello world from the console!")
end

function love.update(dt)
	console.update(dt)
end

function love.keypressed(key)
	console.keypressed(key)
end

function love.draw()
	console.draw()
end
If you have any bugs/suggestions leave a reply. :awesome:

Re: Patman's Console Library

Posted: Mon May 20, 2013 12:35 am
by Kingdaro
Very nice! Though you should probably localize the script's "buffer" variable, as it could become problematic if someone's game used a variable called "buffer". I just added the line

Code: Select all

local buffer
after line 24.

I also thought it would be worth mentioning that it's safe to call console.stealth() from inside love.load (for the sake of keeping things clean and organized) as long as console.load() is also called.

Re: Patman's Console Library

Posted: Mon May 20, 2013 4:17 am
by Patman
Kingdaro wrote:Very nice! Though you should probably localize the script's "buffer" variable, as it could become problematic if someone's game used a variable called "buffer". I just added the line

Code: Select all

local buffer
after line 24.
You're right. I removed it when I was debugging a problem. Should be fixed now.

Re: Patman's Console Library

Posted: Wed May 22, 2013 4:30 pm
by IndieRetro
This is exactly what I was looking for, thank you! Works perfectly, simplistic but awesome! :D

Re: Patman's Console Library

Posted: Thu May 23, 2013 12:20 pm
by SiENcE
You should reset the font and color afer you changed it.

Re: Patman's Console Library

Posted: Wed May 29, 2013 1:53 am
by Patman
SiENcE wrote:You should reset the font and color afer you changed it.
While I think it's a good idea not to make assumptions about the graphics state after some library's draw function is called, the stealth mode makes it non-obvious that it happens so this is a good idea. I've made it work like this now.

I have updated the library so it can be drawn on a canvas and resets the colour and font.