Page 1 of 1

Trouble with variables and love.graphics.print [SOLVED]

Posted: Tue Apr 15, 2014 11:43 am
by ErvinGamez
Hello, I created an account just for this.
I am having some trouble with variables and love.graphics.print, not much more to say. Now I won't post my whole code, here are the important bits:

Code: Select all

function love.draw()
    love.graphics.print("moves:" .. add, 100, 100)
    end

Code: Select all

function love.load()
    player = {
        add = 0,
    }

Code: Select all

function love.keypressed( key )
   if key == "a" then
      add = add + 1
   end
end
And I keep getting this error message:
Error

main.lua:34: attempt to concatenate global 'add' (a nil value)


Traceback

main.lua:34: in function 'draw;
[C]: in function 'xpcall'
Pretty sure it's got something to do with the love.draw code.

Re: Trouble with variables and love.graphics.print

Posted: Tue Apr 15, 2014 12:02 pm
by veethree
You put the "add" variable into the player table. So to use it in the code you need to do "player.add" not just "add".

Code: Select all

love.graphics.print("moves:" .. player.add, 100, 100)

Re: Trouble with variables and love.graphics.print

Posted: Tue Apr 15, 2014 12:05 pm
by HugoBDesigner
You should have posted this in "Support and Development", not here. But this is just technical detail. Well, if that's how you're managing it in your game, I can pretty much see what you are doing wrong: you're trying to change a variable that doesn't exists. Okay, you can see it there on love.load, but the game doesn't. It is inside a table, which means that the variable should always be associated to the table. In your code, replace all the "add" that is alone by "player.add" or "player["add"]". This way, you'll be telling the game that the right variable to look for is inside the table "player", and not by itself :)

AAAAND someone posted before me. Pretty much the same thing ;)

Re: Trouble with variables and love.graphics.print

Posted: Tue Apr 15, 2014 12:14 pm
by ErvinGamez
HugoBDesigner wrote:You should have posted this in "Support and Development", not here. But this is just technical detail. Well, if that's how you're managing it in your game, I can pretty much see what you are doing wrong: you're trying to change a variable that doesn't exists. Okay, you can see it there on love.load, but the game doesn't. It is inside a table, which means that the variable should always be associated to the table. In your code, replace all the "add" that is alone by "player.add" or "player["add"]". This way, you'll be telling the game that the right variable to look for is inside the table "player", and not by itself :)

AAAAND someone posted before me. Pretty much the same thing ;)
veethree wrote:You put the "add" variable into the player table. So to use it in the code you need to do "player.add" not just "add".

Code: Select all

love.graphics.print("moves:" .. player.add, 100, 100)
Thanks, both of you!