Patman's Console Library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Patman
Prole
Posts: 3
Joined: Sun May 19, 2013 6:10 pm

Patman's Console Library

Post 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:
Last edited by Patman on Wed May 29, 2013 1:53 am, edited 2 times in total.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Patman's Console Library

Post 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.
Patman
Prole
Posts: 3
Joined: Sun May 19, 2013 6:10 pm

Re: Patman's Console Library

Post 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.
IndieRetro
Citizen
Posts: 51
Joined: Mon Apr 15, 2013 8:21 pm
Contact:

Re: Patman's Console Library

Post by IndieRetro »

This is exactly what I was looking for, thank you! Works perfectly, simplistic but awesome! :D
irc.freenode.org ##oodnet
http://exez.in/
User avatar
SiENcE
Party member
Posts: 805
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Patman's Console Library

Post by SiENcE »

You should reset the font and color afer you changed it.
Patman
Prole
Posts: 3
Joined: Sun May 19, 2013 6:10 pm

Re: Patman's Console Library

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

Who is online

Users browsing this forum: No registered users and 1 guest