Storing User Text Input

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
groovsmooth
Prole
Posts: 1
Joined: Sun Feb 19, 2023 4:54 am

Storing User Text Input

Post by groovsmooth »

Hi! I'm extremely new to both Lua and LOVE and I'm struggling a bit with the concept of storing a user's text input. I've tried googling and looking on the forums, but I cannot find a clear answer/I am struggling to understand and implement this. Here is a basic idea of what I'm trying to do:

*The game wants to use your username/name for a more personalized experience*
1. Prompt the user to type their username
2. Allow the user to type their name
3. When they press enter, store their inputted name as a variable
4. Print a message including their username

In my head it looks something like this (where "Bertram" has been inputted by the user):
What is your name? Bertram
Hello Bertram, welcome to the game!

I have steps 1-2 down (using the full code snippet at https://love2d.org/wiki/love.textinput), but I can't find a way to store the user's text input/name as a variable to use. It seems really easy to do in theory but I just can't seem to figure it out. If this is a more complex DIY problem, are there any libraries that could help me achieve this? I've been researching this too with no luck thus far :?

Thank you so much in advance :) !!!
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Storing User Text Input

Post by Bigfoot71 »

Here is an extremely basic example:

Code: Select all

function love.load()
    text = ""
    font = love.graphics.newFont(20)
end

function love.draw()
    love.graphics.setFont(font)
    love.graphics.print("Write what you want :", 50, 50)
    love.graphics.rectangle("line", 50, 100, 400, 50)
    love.graphics.print(text, 60, 110)
end

function love.textinput(t)
    text = text .. t
end

function love.keypressed(key)
    if key == "backspace" then
        text = text:sub(1, #text - 1)
    end
end
You can adapt it to your sauce now or use a library for that:https://github.com/love2d-community/awesome-love2d#ui

Although if you're just starting out I really encourage you to write your own based on the example I've shared with you, it will do just what you need and it will make you feel good :awesome:

Edit: Excuse me, I didn't even bother to go see the example of the wiki which is almost identical to mine. Where exactly are you blocking?

If this is how to store the variable either in my example or the wiki one it is just the text variable.

To apologize for my inatention, I quickly gave you a more concrete example that should meet your expectations (my code is not optimized, it's just an example):
Image

If my code is missing comments to help you understand everything, don't hesitate!
Attachments
Text-Input.love
(1.26 KiB) Downloaded 60 times
My avatar code for the curious :D V1, V2, V3.
User avatar
nehu
Prole
Posts: 11
Joined: Fri Feb 25, 2022 1:12 am
Location: Argentina, Maybe under your bed

Re: Storing User Text Input

Post by nehu »

Making a text input might be a pain head :x . If you want to keep it easy, just use love.textinput and add the backspace function :ultraglee: .
However, i don't understand why are you having troubles with the snipet described in the wiki, if you want to get the variable with the text that the user writed, just use the variable "text" from one of the snipets :monocle: . This is the first snipet:

Code: Select all

function love.load()
    text = "Type away! -- "
end

function love.textinput(t)
    text = text .. t --HERE, THE TEXT VARIABLE IS WHAT YOU ARE LOOKING FOR 
end

function love.draw()
    love.graphics.printf(text, 0, 0, love.graphics.getWidth())
end
as you can see, you already have the variable, if you want to store it on another variable just do something like

Code: Select all

playerName = text
or wathever you want idk lol :crazy:
you already have the variable and you can use it for anything you like, so i don't undertand the problem. ;)

Code: Select all

for i, v in pairs(problems) do
	fix(v)
end
Post Reply

Who is online

Users browsing this forum: coolCatfish, Majestic-12 [Bot], Semrush [Bot] and 1 guest