[SOLVED]Interactive dialog and character naming
Posted: Tue Jan 26, 2016 9:57 pm
Hey guys! Uber-noob here. Just started learning love2d a week ago. I seem to be doing good, didn't have any trouble following tutorials... but there's this one thing, a very simple thing, which I can't figure out at all! Never even found any data on it using google, the wiki, or this forum (maybe I'm just bad at searching? God knows I tried!)
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
But this does not work in Love, it just posted the question and offers input in the debug console, not on my game screen ;((((
What I tried is:
But it dudn't work! Please help, guys! Just point me to the right direction!
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