Page 1 of 1
Inputting a name
Posted: Tue Oct 20, 2009 5:45 pm
by SamKablam
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!
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:03 pm
by Robin
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:
Code: Select all
function keypressed(key)
if key ~= love.key_enter then
Name = Name .. string.char(key)
end
end
Thirdly, for things like this, you might want to use a GUI library -- which LÖVE has in abundance.
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:10 pm
by SamKablam
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
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:16 pm
by bartbes
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++.
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:24 pm
by SamKablam
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
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:31 pm
by bartbes
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.
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:35 pm
by SamKablam
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.
Re: Inputting a name
Posted: Tue Oct 20, 2009 6:35 pm
by Robin
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?
Concatenate means adding two strings together. However, you can't add a string to nil.
So this:
should be
Please note that it is by no means stable (again, there are a plethora of GUI libs available for LÖVE).
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.