Page 1 of 1

How to get text input and displaying it?

Posted: Mon May 20, 2013 8:44 pm
by Eamonn
I understand the concept on how to get input, but the way I was thinking of doing it (and it works, may I add) was very inefficient. Heres a sample:

Code: Select all

function love.load()
    text = ""
end

function love.update()

end

function love.draw()
    love.graphics.print(text, 100, 10)
end

function love.keypressed(key)
    if key == "a" or "b" or "c" then -- and so on
        text = key
    end
end
This, I'm sure, is a very bad way of doing it. And if you had to enter a name for a save file and had to put that in like 5 times if you had a limit of 5 letters? That would be ridiculous. I saw a library called 'TextInput', but it was for 0.7.X. If you know about that library, could you maybe tell me if it still works for 0.8.0 or if there is an updated version of it? Is there another library like it? Is there a solution? The above code was the only solution I can think of.

Any help is appreciated! I've gotten a lot of help from people on this forum, so hopefully my luck continues :D

Re: How to get text input and displaying it?

Posted: Mon May 20, 2013 9:21 pm
by Larsii30
You could use lua's match function.
( you can find it here: http://lua-users.org/wiki/StringLibraryTutorial )

basically do something like this :

Code: Select all


local input = ""

function love.keypressed(key)
     if key and key:match( '^[%w%s]$' ) then input = input..key end
end

Then simply draw the string where you want.

This adds the pressed key to a string. What the '^[%w%s]$' means can be found here :
http://www.lua.org/manual/5.1/manual.html#5.4.1

Re: How to get text input and displaying it?

Posted: Mon May 20, 2013 9:23 pm
by Eamonn
Ah. Thank's! The links were very helpful!

Re: How to get text input and displaying it?

Posted: Mon May 20, 2013 9:25 pm
by veethree

Re: How to get text input and displaying it?

Posted: Mon May 20, 2013 9:26 pm
by Larsii30
ups sorry , had to correct myself.

Code: Select all


local input = ""

function love.keypressed(key)
     if key and key:match( '^[%w%s]$' ) then input = input..key end
end