How to get text input and displaying it?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

How to get text input and displaying it?

Post by Eamonn » Mon May 20, 2013 8:44 pm

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
"In those quiet moments, you come into my mind" - Liam Reilly

User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: How to get text input and displaying it?

Post by Larsii30 » Mon May 20, 2013 9:21 pm

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
Last edited by Larsii30 on Mon May 20, 2013 9:31 pm, edited 2 times in total.

User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: How to get text input and displaying it?

Post by Eamonn » Mon May 20, 2013 9:23 pm

Ah. Thank's! The links were very helpful!
"In those quiet moments, you come into my mind" - Liam Reilly

User avatar
veethree
Inner party member
Posts: 814
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to get text input and displaying it?

Post by veethree » Mon May 20, 2013 9:25 pm


User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: How to get text input and displaying it?

Post by Larsii30 » Mon May 20, 2013 9:26 pm

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


Post Reply

Who is online

Users browsing this forum: raidho36, Sky_Render and 19 guests