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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
externalerror
Prole
Posts: 1
Joined: Fri Mar 01, 2013 3:36 pm

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

Post 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
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

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

Post 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
Shallow indentations.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

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

Post by Taehl »

-snip-
Whatever.
Last edited by Taehl on Sun Mar 03, 2013 2:46 am, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

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

Post 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.
Shallow indentations.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

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

Post by Lafolie »

Also the total amount of time passed (since the initial execution of your program) is returned from love.timer.getTime.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests