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 !!!
Storing User Text Input
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Storing User Text Input
Here is an extremely basic example:
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
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):
If my code is missing comments to help you understand everything, don't hesitate!
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
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
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):
If my code is missing comments to help you understand everything, don't hesitate!
- Attachments
-
- Text-Input.love
- (1.26 KiB) Downloaded 60 times
Re: Storing User Text Input
Making a text input might be a pain head . If you want to keep it easy, just use love.textinput and add the backspace function .
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 . This is the first snipet:
as you can see, you already have the variable, if you want to store it on another variable just do something like
or wathever you want idk lol
you already have the variable and you can use it for anything you like, so i don't undertand the problem.
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 . 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
Code: Select all
playerName = text
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
Who is online
Users browsing this forum: coolCatfish, Majestic-12 [Bot], Semrush [Bot] and 1 guest