So, anyway, what I need is to create a dialogue between my game and the player. It should go like this:
Game: Give your character a name!
Player now can input text into a rectangle. After he writes his name, Jim for example, he presses "enter", and the game says:
"Do you want your character to be named Jim?" Y/N
If the player chooses Y, he proceeds, and his character is called Jim in the game.
If N, the player gets to input text into a rectangle again!
How do I do it? I know the LUA code for it, something like
Code: Select all
io.write('Hello, what is your name? ')
local name = io.read()
io.write('So your name is, ', name, '!\n')
What I tried is:
Code: Select all
local utf8 = require("utf8")
function love.load()
text = "Type away! -- "
-- enable key repeat so backspace can be held down to trigger love.keypressed multiple times.
love.keyboard.setKeyRepeat(true)
end
function love.textinput(t)
text = text .. t
end
function love.update(dt)
end
function love.keypressed(key)
if key == "backspace" then
-- get the byte offset to the last UTF-8 character in the string.
local byteoffset = utf8.offset(text, -1)
if byteoffset then
-- remove the last UTF-8 character.
-- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
text = string.sub(text, 1, byteoffset - 1)
end
end
end
function love.draw()
love.graphics.printf(text, 0, 0, love.graphics.getWidth())
if key == "space" then
love.graphics.printf("so your name is #{text}", 0, 30, love.graphics.getWidth())
end
end