Inputting a name

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
SamKablam
Prole
Posts: 4
Joined: Tue Oct 20, 2009 5:39 pm

Inputting a name

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Inputting a name

Post 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.
Help us help you: attach a .love.
SamKablam
Prole
Posts: 4
Joined: Tue Oct 20, 2009 5:39 pm

Re: Inputting a name

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Inputting a name

Post 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++.
SamKablam
Prole
Posts: 4
Joined: Tue Oct 20, 2009 5:39 pm

Re: Inputting a name

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Inputting a name

Post 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

Code: Select all

str_Name = ""
it should work.
SamKablam
Prole
Posts: 4
Joined: Tue Oct 20, 2009 5:39 pm

Re: Inputting a name

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Inputting a name

Post 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:

Code: Select all

str_Name = nil
should be

Code: Select all

str_Name = ""
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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 13 guests