Page 1 of 1

update() and draw() dont work together like expected

Posted: Sat Mar 02, 2013 6:09 am
by externalerror
Hello dear helpful and lovely lövepeople,

I want to start writing an application with alternating framebuffer, but already at the first step I am confused.

a example from the wiki (slightly expanded) don't work like expected:

Code: Select all

dtotal = 0   -- this keeps track of how much time has passed
function love.update(dt)
   dtotal = dtotal + dt   -- we add the time passed since the last update, probably a very small number like 0.01
   if dtotal >= 1 then
		print_a_frame()
		dtotal = dtotal - 1   -- reduce our timer by a second, but don't discard the change... what if our framerate is 2/3 of a second?
   end 
end

print_a_frame = function()
	function love.draw()
		love.graphics.print(  math.random(1,100), 10, 20)
	end
end
What i expect what should happen:
An endless loop prints exactly every second a random number on the screen

What happened:
After an initial waiting period of one second (adjustiable by dtotal > 1) an endless loop prints as fast as it can (many times a second) a random number on the screen.

Can anyone explaine me that.

Greetings
ee

Re: update() and draw() dont work together like expected

Posted: Sat Mar 02, 2013 7:12 am
by Boolsheet
The default main loop of LÖVE first calls love.update, then clears the screen and finally calls love.draw. See love.run for the full code.

Your print_a_frame function creates the same function over and over again and assigns it to love.draw. LÖVE calls love.draw for every frame and because you use math.random in there it will print a new random number every time.

You probably want to save a new random number when the timer even triggers. The love.draw code then just has to check if it has to draw something. You don't necessarily have to do it this way, but separating update logic from drawing can simplify things if the project gets larger.

Code: Select all

function save_random_number()
	random_number = math.random(1, 100)
end

function love.load()
	dtotal = 0
	save_random_number()
end

function love.update(dt)
	dtotal = dtotal + dt
	if dtotal >= 1 then
		save_random_number()
		dtotal = dtotal - 1
	end 
end

function love.draw()
	if random_number then
		love.graphics.print(random_number, 10, 20)
	end	
end

Re: update() and draw() dont work together like expected

Posted: Sat Mar 02, 2013 7:00 pm
by Taehl
-snip-
Whatever.

Re: update() and draw() dont work together like expected

Posted: Sat Mar 02, 2013 10:25 pm
by Boolsheet
Taehl, I think you need to look at my code again. It looks like you misread it.

Your code still recreates the same function over and over again. If you're going to say he should use your code instead, don't add the other issues back in again.

Re: update() and draw() dont work together like expected

Posted: Tue Mar 05, 2013 1:25 pm
by Lafolie
Also the total amount of time passed (since the initial execution of your program) is returned from love.timer.getTime.