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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
ErvinGamez
Prole
Posts: 17
Joined: Tue Apr 15, 2014 11:32 am

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

Post 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.
Last edited by ErvinGamez on Tue Apr 15, 2014 2:27 pm, edited 1 time in total.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Trouble with variables and love.graphics.print

Post 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)
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Trouble with variables and love.graphics.print

Post 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 ;)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
ErvinGamez
Prole
Posts: 17
Joined: Tue Apr 15, 2014 11:32 am

Re: Trouble with variables and love.graphics.print

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 2 guests