ConsoleBox!

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Konidem
Prole
Posts: 3
Joined: Sat Jan 31, 2015 12:32 pm

ConsoleBox!

Post by Konidem »

Hi! I create this 'script' because whenever I do a game and I need "in game console" I had a problem with scrolling text(Write all times same script)... Whatever, it's still in beta but it working right now and it's easy to implement into YOUR AWESOME GAME!

Using:

Code: Select all

require 'consolebox'
function love.load()
    cb.load()
end

function love.draw()
    cb.draw()
end
To add message to box use:

Code: Select all

cb.addMs(put string here)
Code of consolebox.lua:

Code: Select all

local utf8 = require("utf8")
cb = {}
function cb.load()
	messages = {}
	ms = -15
	ms2 = 0
	input = ""
end

function cb.draw()
	if messages[ms] then love.graphics.print(messages[ms], 5, 5) end
	if messages[ms+1] then love.graphics.print(messages[ms+1], 5, 20) end
	if messages[ms+2] then love.graphics.print(messages[ms+2], 5, 35) end
	if messages[ms+3] then love.graphics.print(messages[ms+3], 5, 50) end
	if messages[ms+4] then love.graphics.print(messages[ms+4], 5, 65) end
	if messages[ms+5] then love.graphics.print(messages[ms+5], 5, 80) end
	if messages[ms+6] then love.graphics.print(messages[ms+6], 5, 95) end
	if messages[ms+7] then love.graphics.print(messages[ms+7], 5, 110) end
	if messages[ms+8] then love.graphics.print(messages[ms+8], 5, 125) end
	if messages[ms+9] then love.graphics.print(messages[ms+9], 5, 140) end
	if messages[ms+10] then love.graphics.print(messages[ms+10], 5, 155) end
	if messages[ms+11] then love.graphics.print(messages[ms+11], 5, 170) end
	if messages[ms+12] then love.graphics.print(messages[ms+12], 5, 185) end
	if messages[ms+13] then love.graphics.print(messages[ms+13], 5, 200) end
	if messages[ms+14] then love.graphics.print(messages[ms+14], 5, 215) end
	if messages[ms+15] then love.graphics.print(messages[ms+15], 5, 230) end
	love.graphics.print(input, 5, 255)
end

function cb.addMs(message)
	messages[ms2] = message
	if ms2 > ms-15 then ms = ms+1 end
	ms2 = ms2+1
end

function cb.mp(x,y, button)
  if button == "wu" then
    ms = ms - 1
  elseif button == "wd" then
    ms = ms + 1
  end
end

function cb.kp(key)
	if key == "return" then
		messages[ms2] = input
		if ms2 > ms-15 then ms = ms+1 end
		ms2 = ms2+1
		input = ""
	elseif key == "backspace" then
		byteoffset = utf8.offset(input, -1)
		input = string.sub(input, 1, byteoffset - 1)
	else
		input = input..key
	end
end
If you want to add scrolling option implement

Code: Select all

cb.mp(x, y, key)
into

Code: Select all

function love.mousepressed(x, y, key)
To give user option for writing just add

Code: Select all

cb.kp(key)
into

Code: Select all

function love.keypressed(key)
And this is it. You can edit this code if you want to work different. And remember this is on beta, not all works perfect!
Last edited by Konidem on Sun Sep 13, 2015 8:11 am, edited 1 time in total.
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: ConsoleBox!

Post by alberto_lara »

I want to try it out but.. too lazy to do all the work, could you please create a .love with a demo? Thanks!
Konidem
Prole
Posts: 3
Joined: Sat Jan 31, 2015 12:32 pm

Re: ConsoleBox!

Post by Konidem »

alberto_lara wrote:I want to try it out but.. too lazy to do all the work, could you please create a .love with a demo? Thanks!
Thanks for reply! I add demo for actual version. :3
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: ConsoleBox!

Post by alberto_lara »

Thanks for the .love file, I haven't checked all the code but you could change this:

Code: Select all

if messanges[ms] then love.graphics.print(messanges[ms], 5, 5) end
if messanges[ms+1] then love.graphics.print(messanges[ms+1], 5, 20) end
if messanges[ms+2] then love.graphics.print(messanges[ms+2], 5, 35) end
if messanges[ms+3] then love.graphics.print(messanges[ms+3], 5, 50) end
if messanges[ms+4] then love.graphics.print(messanges[ms+4], 5, 65) end
if messanges[ms+5] then love.graphics.print(messanges[ms+5], 5, 80) end
if messanges[ms+6] then love.graphics.print(messanges[ms+6], 5, 95) end
if messanges[ms+7] then love.graphics.print(messanges[ms+7], 5, 110) end
if messanges[ms+8] then love.graphics.print(messanges[ms+8], 5, 125) end
if messanges[ms+9] then love.graphics.print(messanges[ms+9], 5, 140) end
if messanges[ms+10] then love.graphics.print(messanges[ms+10], 5, 155) end
if messanges[ms+11] then love.graphics.print(messanges[ms+11], 5, 170) end
if messanges[ms+12] then love.graphics.print(messanges[ms+12], 5, 185) end
if messanges[ms+13] then love.graphics.print(messanges[ms+13], 5, 200) end
if messanges[ms+14] then love.graphics.print(messanges[ms+14], 5, 215) end
if messanges[ms+15] then love.graphics.print(messanges[ms+15], 5, 230) end
for this:

Code: Select all

local step = 15
for i = 0, 15 do
	if messanges[ms + i] then love.graphics.print(messanges[ms + i], 5, step) end
	step = step + 15
end
Maybe you're kind of new to programming, if that's the case, then Lua/LÖVE is a good start, go on :)

There are similar projects to this here in he forum like this one: viewtopic.php?f=5&t=2473
or this one I made a while ago (more like a variable-window-inspector, not a console, and the code is sort of a mess): viewtopic.php?f=5&t=77571

You could check that out and get a little more into it.
Konidem
Prole
Posts: 3
Joined: Sat Jan 31, 2015 12:32 pm

Re: ConsoleBox!

Post by Konidem »

Maybe you're kind of new to programming
Yes, it's true, I'm still learning :awesome:
local step = 15
for i = 0, 15 do
if messanges[ms + i] then love.graphics.print(messanges[ms + i], 5, step) end
step = step + 15
end
Thanks! I don't understand for loop in LOVE/LUA (Maybe because I start from Java and C++, who know, after one month I changed this for LOVE but witchout any tutorials, Wiki only :D )
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: ConsoleBox!

Post by alberto_lara »

Yes, it's true, I'm still learning
We all are still learning, we're just in different stages, keep the good work :)

The Lua for is quite simple actually, this:

Code: Select all

for i = 0, 15 do
means "go from zero to 15, with a step = 1"

and if you put:

Code: Select all

for i = 0, 15, 2 do
this means the same, with a step = 2, so your i index will go 0, 2, 4, 6, 8...

If you started with java and C++, I think it's a good tip to know that arrays in Lua start with index = 1, unlike other languages.

e.g if you do:

Code: Select all

t = {144, 2, 3, 4}
print(t[1])
the output will be '144' because t[0] is nil (similar to null or undefined).

EDIT: To be accurate, there's no such thing as an array itself in Lua, we have tables (like the above one), and that's it. The good thing is tables are very flexible and you can take them as arrays, lists, etc.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests