[SOLVED]Interactive dialog and character naming

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
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

[SOLVED]Interactive dialog and character naming

Post by fade »

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

Code: Select all

io.write('Hello, what is your name? ')
local name = io.read()
io.write('So your name is, ', name, '!\n')
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:

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
But it dudn't work! Please help, guys! Just point me to the right direction!
Last edited by fade on Wed Jan 27, 2016 10:31 pm, edited 1 time in total.
User avatar
HaftSwimmingly
Prole
Posts: 13
Joined: Fri Dec 11, 2015 8:13 am

Re: Interactive dialog and character naming

Post by HaftSwimmingly »

Welcome to the forums fam!

I Think I can help here!

Basically this is what it will look like

Code: Select all


local utf8 = require("utf8")

function love.load()
  
  --Because i'm lazy
  gr = love.graphics
  kb = love.keyboard
  
  name = ""
  nameinput = "on"
  
  
end
function love.textinput(t)
  
  if nameinput == "on" then
  name = name .. t
  end
  if nameinput == "off" then
  name = name
  end

  
end
function love.draw()
  
  gr.print("GIVE YOUR CHARACTER A NAME!")
  gr.printf(name, 0, 100,400, "center")
  
  if nameinput == "off" then
    gr.print("SO YOUR CHARACTER'S NAME IS "..name.."?", 0,400)
    gr.print("Y/N",0,430)
  end
  
end
function love.keypressed(key)
  if kb.isDown(" ") then -- I don't remember what the input for enter is, so i'm just using space. Change if need be :)
    nameinput = "off"
  end
  if kb.isDown('y') and nameinput == "off" then
    love.event.quit()
    --Change this with whatever you want it to do next
    -- Remember that "name" will be its variable
  end
  if kb.isDown('n') and nameinput == "off" then
    nameinput = "on"
  end
end
function love.update(dt)
  -- Put this backspace stuff so it automatically repeats the key presses
  -- No need for kb.setKeyRepeat(true)
      if kb.isDown('backspace') then
        local byteoffset = utf8.offset(name, -1)
        if byteoffset then
            name = string.sub(name, 1, byteoffset - 1)
        end
    end
end
Now you may notice that the backspace key is fast with the erasing. You, of course, can always move it back to love.keypressed, but it won't erase by holding. I personally find this to be best for short names, but i assume you want otherwise.
tomaki on IRC
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

Re: Interactive dialog and character naming

Post by fade »

HaftSwimmingly wrote:Welcome to the forums fam!

I Think I can help here!

Basically this is what it will look like

Code: Select all


local utf8 = require("utf8")

function love.load()
  
  --Because i'm lazy
  gr = love.graphics
  kb = love.keyboard
  
  name = ""
  nameinput = "on"
  
  
end
function love.textinput(t)
  
  if nameinput == "on" then
  name = name .. t
  end
  if nameinput == "off" then
  name = name
  end

  
end
function love.draw()
  
  gr.print("GIVE YOUR CHARACTER A NAME!")
  gr.printf(name, 0, 100,400, "center")
  
  if nameinput == "off" then
    gr.print("SO YOUR CHARACTER'S NAME IS "..name.."?", 0,400)
    gr.print("Y/N",0,430)
  end
  
end
function love.keypressed(key)
  if kb.isDown(" ") then -- I don't remember what the input for enter is, so i'm just using enter. Change if need be :)
    nameinput = "off"
  end
  if kb.isDown('y') and nameinput == "off" then
    love.event.quit()
    --Change this with whatever you want it to do next
    -- Remember that "name" will be its variable
  end
  if kb.isDown('n') and nameinput == "off" then
    nameinput = "on"
  end
end
function love.update(dt)
  -- Put this backspace stuff so it automatically repeats the key presses
  -- No need for kb.setKeyRepeat(true)
      if kb.isDown('backspace') then
        local byteoffset = utf8.offset(name, -1)
        if byteoffset then
            name = string.sub(name, 1, byteoffset - 1)
        end
    end
end
Now you may notice that the backspace key is fast with the erasing. You, of course, can always move it back to love.keypressed, but it won't erase by holding. I personally find this to be best for short names, but i assume you want otherwise.
Oh wow! You're a life-saver! I would've never figured this one out just by manual alone! The backspace trick is great by the way, I think I'll use it in some places :D Thanks a lot! Never even hoped for a quick solution. Now, based on this, I''ll make so much different dialogues, wow, just wow! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests