Page 1 of 1

New to LÖVE, need some help with basic printing

Posted: Sat Mar 28, 2015 8:53 pm
by NigelNoscopes
I know a sufficient amount of Lua to make things work. I'd like to have an option (similar to Minecraft) where the user can press a key and toggle a print function that prints debug info, such as the x and y coordinates of the player. I tried doing it like this:

Code: Select all

function love.keyboard.isDown('p')
    love.graphics.print('player.x = ' .. player.x .. '\nplayer.y = ' .. player.y, 0, 0)
end
This function is under the

Code: Select all

function love.update(dt)
block. When I press that key, though, it doesn't print anything. What am I doing wrong?

Thanks in advance for the help.

Re: New to LÖVE, need some help with basic printing

Posted: Sat Mar 28, 2015 10:23 pm
by s-ol
NigelNoscopes wrote:I know a sufficient amount of Lua to make things work. I'd like to have an option (similar to Minecraft) where the user can press a key and toggle a print function that prints debug info, such as the x and y coordinates of the player. I tried doing it like this:

Code: Select all

function love.keyboard.isDown('p')
    love.graphics.print('player.x = ' .. player.x .. '\nplayer.y = ' .. player.y, 0, 0)
end
This function is under the

Code: Select all

function love.update(dt)
block. When I press that key, though, it doesn't print anything. What am I doing wrong?

Thanks in advance for the help.
you are mixing up overwriting callbacks and calling functions. You want to call "love.keyboard.isDown('p')", which will return true or false to indicate whether 'p' is pressed, then draw or not draw based on that:

Code: Select all

function love.draw() -- defining the function "love.draw"
  -- draw other stuff
  if love.keyboard.isDown('p') then -- call "love.keyboard.isDown" with parameter 'p'
    love.graphics.print("debuginfo", 0,0)
  end
end

Re: New to LÖVE, need some help with basic printing

Posted: Sat Mar 28, 2015 10:27 pm
by Kingdaro
Look at the example on the page for proper usage. Also, all graphics functions have to go under love.draw, not update. This is what you want:

Code: Select all

local debugInfo = false

function love.update(dt)
   debugInfo = love.keyboard.isDown('p')
   -- the above is the same as:
   -- if love.keyboard.isDown('p') then
   --   debugInfo = true
   -- else
   --   debugInfo = false
   -- end
end

function love.draw()
   if debugInfo then
      love.graphics.print('player.x = ' .. player.x .. '\nplayer.y = ' .. player.y, 0, 0)
   end
end
I would also recommend reading the PIL, for a better understanding of Lua. If that doesn't help you, look for a more intuitive Lua tutorial that you might understand a little more.

EDIT: Didn't even see the post above me. Fuck everything.

Re: New to LÖVE, need some help with basic printing

Posted: Sun Mar 29, 2015 12:40 am
by NigelNoscopes
Kingdaro wrote:Look at the example on the page for proper usage. Also, all graphics functions have to go under love.draw, not update. This is what you want:

Code: Select all

local debugInfo = false

function love.update(dt)
   debugInfo = love.keyboard.isDown('p')
   -- the above is the same as:
   -- if love.keyboard.isDown('p') then
   --   debugInfo = true
   -- else
   --   debugInfo = false
   -- end
end

function love.draw()
   if debugInfo then
      love.graphics.print('player.x = ' .. player.x .. '\nplayer.y = ' .. player.y, 0, 0)
   end
end
I would also recommend reading the PIL, for a better understanding of Lua. If that doesn't help you, look for a more intuitive Lua tutorial that you might understand a little more.

EDIT: Didn't even see the post above me. Fuck everything.
This helped me. The above did, as well. Thanks to both of you. :)