I must be really, really, REALLY stupid. For four hours I've been trying to find a good explanation or tutorial (video or text) on how to enter a text string into a text variable from the keyboard, i.e., basically perform the ioread() function of lua. But I haven't found any.
What I basically want to do is create a questionaire where the program:
1. prints a question
2. the user types in an answer
3. and then clicks a submit button
4. the program prints the next question and this continues until the last question is answered.
5. the program prints the questions and the user's answers to the screen and says "Thanks for participating."
I know how to print the questions, but I can't figure out how to then wait for the users typed input and mouse input to click the submit button. Is there a tutorial or explanation of how to do this somewhere?
Thanks.
Randall
How to enter a text string into a text variable from the keyboard
Re: How to enter a text string into a text variable from the keyboard
This is very primitive input (and button) but I hope it'll help.
Basically don't think in terms of waiting as the game should not wait (not in a way a cli apps does with io.read()) - it rather reacts to events and you just hold some state (if needed) in a variable. Read about love.textinput and love.keypressed callbacks.
Basically don't think in terms of waiting as the game should not wait (not in a way a cli apps does with io.read()) - it rather reacts to events and you just hold some state (if needed) in a variable. Read about love.textinput and love.keypressed callbacks.
Code: Select all
local questions = {
'What is your name?',
'What is your quest?',
'What is the airspeed velocity of an unladen swallow?'
}
local finished = false
local answear = ''
local current_question_pos = 1
function proceed()
if answear ~= '' then -- let's disallow empty answers
print(answear) -- or save somehow, somewhere
answear = ''
current_question_pos = current_question_pos + 1
if current_question_pos > #questions then
finished = true
end
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
if finished then
love.graphics.print('Thanks for participating.', 10, 30)
else
love.graphics.print('Q: ' .. questions[current_question_pos], 10, 30)
love.graphics.print('A: ' .. answear, 10, 60)
love.graphics.rectangle('fill', 10, 100, 90, 30)
love.graphics.setColor(0, 0, 0)
love.graphics.printf('next', 10, 110, 90, 'center')
end
end
function love.mousepressed(x, y, btn)
if (10 < x and x < 100) and (100 < y and y < 130) then
proceed()
end
end
function love.keypressed(key)
if key == 'backspace' then answear = answear:sub(1,-2) end
if key == 'return' then proceed() end
end
function love.textinput(text)
answear = answear .. text
end
Re: How to enter a text string into a text variable from the keyboard
A text entry field is a "widget" in GUI terms. The easy way to get text input fields and buttons is to use a GUI library. There are a number around.
Or you can go the DIY way like 0x72 suggests, but things may get complicated if you want it not to crash when deleting non-ASCII characters such as ñ. Most GUI libraries take good care of handling that gracefully.
Or you can go the DIY way like 0x72 suggests, but things may get complicated if you want it not to crash when deleting non-ASCII characters such as ñ. Most GUI libraries take good care of handling that gracefully.
Re: How to enter a text string into a text variable from the keyboard
Thanks for yourhelp, guys.
Who is online
Users browsing this forum: No registered users and 2 guests