Hello!
I'm new to LOVE LUA and I'm trying to make my first project. It's just going to be a simple text-based adventure and I wanted to start it off with the player inputting their name. So far, I'm having trouble figuring out how to do this, and when the function should be called. So far, it looks like this:
array_Name = {}
int_nameDraw = 50
function draw()
love.graphics.draw("Welcome to Super Text and Graphics Quest!! Enter your name now!", 25, 25)
love.graphics.draw(array_Name, int_nameDraw, 50)
end
function keypressed(key)
for i = 1,i++ do
if key ~= love.key_enter then
array_Name = key
int_NameDraw = int_NameDraw+5
end
end
end
Please let me know if there's an easier way to do this, thanks!
Inputting a name
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Inputting a name
Remove the for loop. keypressed is called by LÖVE every time a key is pressed, so you don't have to have a loop.
And an easier way than using a table for this would be using a string. You could simply do:
Thirdly, for things like this, you might want to use a GUI library -- which LÖVE has in abundance.
And an easier way than using a table for this would be using a string. You could simply do:
Code: Select all
function keypressed(key)
if key ~= love.key_enter then
Name = Name .. string.char(key)
end
end
Help us help you: attach a .love.
Re: Inputting a name
Ok, I tried removing the loop, replacing it with what you wrote, and then having the draw function type the Name variable, but all I got was an error. Here's what I have now:
Code: Select all
function load()
defaultfont = love.graphics.newFont(love.default_font, 14);
fontfile = love.graphics.newFont("LOTR.TTF", 14);
love.graphics.setFont(fontfile); -- change the font in here to see the changes
love.graphics.setColor(255,255,255,255)
str_Name = nil
end
function update(dt)
end
function draw()
love.graphics.draw("Welcome to Super Text and Graphics Quest!! Enter your name now!", 25, 25)
love.graphics.draw(str_Name, 50, 50)
end
function keypressed(key)
if key ~= love.key_enter then
str_Name = str_Name .. string.char(key)
end
end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Inputting a name
He said A GUI lib, no THE GUI lib, anyway, you can find them here: http://love2d.org/wiki/index.php?title=Main_Page#GUI
And I would like to note that your for loop was wrong either way, the correct syntax would be for i = begin, end, not for i = begin, i++.
And I would like to note that your for loop was wrong either way, the correct syntax would be for i = begin, end, not for i = begin, i++.
Re: Inputting a name
Ok, so it doesn't get an error immediately. However, when I press a key, I get an error saying "attempt to concatenate (wtf does that mean?) global 'Name' (a nil value). So what's wrong, can I get it to display the name as the player is typing it?
Code: Select all
function load()
defaultfont = love.graphics.newFont(love.default_font, 14);
fontfile = love.graphics.newFont("LOTR.TTF", 14);
love.graphics.setFont(fontfile); -- change the font in here to see the changes
love.graphics.setColor(255,255,255,255)
str_Name = nil
end
function update(dt)
end
function draw()
love.graphics.draw("Welcome to Super Text and Graphics Quest!! Enter your name now!", 25, 25)
love.graphics.draw(str_Name, 50, 50)
end
function keypressed(key)
if key ~= love.key_enter then
str_Name = str_Name .. string.char(key)
end
end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Inputting a name
Concatenation is when you put to strings together, so it is the .. operator. However, lua can only concatenate string and numbers, not nil, which is the initial value of str_Name, so if you change the line in your load function to say
it should work.
Code: Select all
str_Name = ""
Re: Inputting a name
Thanks! I just figured it out as you posted this. Ok, so now that the player can enter their name, how do you get the game to continue and display new text? I'm still trying to figure out how to trigger certain functions to happen at certain times, rather than constantly such as with Draw and KeyPressed.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Inputting a name
Concatenate means adding two strings together. However, you can't add a string to nil.SamKablam wrote:Ok, so it doesn't get an error immediately. However, when I press a key, I get an error saying "attempt to concatenate (wtf does that mean?) global 'Name' (a nil value). So what's wrong, can I get it to display the name as the player is typing it?
So this:
Code: Select all
str_Name = nil
Code: Select all
str_Name = ""
In 0.6.0 (the next release), keypressed will have another argument, which contains the string for the key that is pressed (if there is any), so you can just add that.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Google [Bot] and 5 guests