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

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
NigelNoscopes
Prole
Posts: 6
Joined: Sat Mar 28, 2015 8:22 pm

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

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

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

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

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

Post 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.
NigelNoscopes
Prole
Posts: 6
Joined: Sat Mar 28, 2015 8:22 pm

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

Post 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. :)
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 4 guests