label is created multiple times?

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
larz
Prole
Posts: 9
Joined: Fri Jan 08, 2016 2:57 pm

label is created multiple times?

Post by larz »

Code: Select all

function love.update(dt)
	if love.mouse.isDown(1) then
		function love.draw()
			love.graphics.setColor(0,255,0,255);
			love.graphics.printf("LMB: "..tostring(input.mouse.down.left),1,1,200,"left");
		end
	end
end
Hello, guys. I'm new in LÖVE2D, but i'm experienced with LUA.
I just want to know if this function will create a lot of labels or just 1.
I guess it's going to create labels, but anyway... i don't know how this function works in LÖVE2D.
Could someone help me? Thank you.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: label is created multiple times?

Post by Nixola »

It's only going to print that variable once every frame; every frame the screen also gets cleared, so it will only make one. Every LÖVE function that prints/draws something to the screen works that way.
Welcome anyway ^^
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: label is created multiple times?

Post by s-ol »

it redefines love.draw every frame though, and the "if" won't work that way; the first time you press the mouse button it sets love.draw to draw the text every frame and you never change it back.

You probably want this:

Code: Select all

function love.draw()
   if love.mouse.isDown(1) then
      love.graphics.setColor(0,255,0,255);
      love.graphics.printf("LMB: "..tostring(input.mouse.down.left),1,1,200,"left");
   end
end
also there are no "labels" and nothing is "created". love.graphics.print[f] just draws something once (one frame) and doesn't create or save anything.

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
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: label is created multiple times?

Post by zorg »

Welcome, indeed!
Also, you shouldn't really define love.draw inside love.update, since it'll redefine it every frame; do it this way instead:

Code: Select all

local trigger = false
function love.update(dt)
  if love.mouse.isDown(1) then
     trigger = true
  else
     trigger = false
  end
end

function love.draw()
  if trigger then
     love.graphics.setColor(0,255,0,255);
     love.graphics.printf("LMB: "..tostring(input.mouse.down.left),1,1,200,"left");
  end
end
Or you could put the mouse button testing inside love.draw too, but that's considered bad practice, since it will combine logic with rendering.
EDIT: Ninjad :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
larz
Prole
Posts: 9
Joined: Fri Jan 08, 2016 2:57 pm

Re: label is created multiple times?

Post by larz »

Alright!
Thanks for the nice reception! :awesome:
Also, how does the window width/height works?
I would like to set a label in the right-top corner of the game screen.
How could i do that?
larz
Prole
Posts: 9
Joined: Fri Jan 08, 2016 2:57 pm

Re: label is created multiple times?

Post by larz »

someone?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: label is created multiple times?

Post by zorg »

really simple imlpementation:

Code: Select all

local label = "I AM NOT A LABEL :3"
local width = love.window.getWidth()
function love.draw()
  love.graphics.printf(label, width, 0, width/2, 'right') -- string, x, y, limit, alignment (and other optional stuff after)
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests