Slower update(dt) function?

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
User avatar
Dangleberries
Prole
Posts: 3
Joined: Fri Jan 01, 2010 11:22 am

Slower update(dt) function?

Post by Dangleberries »

How would I go about writing an update(dt) function that is not called every frame, but let's say every second or so?

I've seen that this is possible in BölderDäsch (http://love2d.org/forum/viewtopic.php?f=5&t=1136 - The rocks physics only tick every half second or so), but that sourcecode is unusually complicated and I could not find it there.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Slower update(dt) function?

Post by bartbes »

The update function is probably still called every frame, but using a counter you can choose on which frames you actually want to do something. (or you can sleep the whole code making it unresponsive)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Slower update(dt) function?

Post by Robin »

Well, you can use your own love.run():

Code: Select all

function love.run()
 	if love.load then love.load() end
	-- Main loop.
	local timepassed = 0
	while true do
		love.timer.step()
		timepassed = timepassed + love.timer.getDelta()
		if love.update and timepassed > 1 then timepassed = timepassed - 1 ; love.update() end
		love.graphics.clear()
		if love.draw then love.draw() end
 		-- Process events.
		for e,a,b,c in love.event.get() do
			if e == love.event_quit then return end
			love.handlers[e](a,b,c)
		end
		love.graphics.present()
	end
end
Disclaimer: untested, blah blah, own risk, blah blah... you get the idea. ;)
Help us help you: attach a .love.
User avatar
Dangleberries
Prole
Posts: 3
Joined: Fri Jan 01, 2010 11:22 am

Re: Slower update(dt) function?

Post by Dangleberries »

bartbes wrote:The update function is probably still called every frame, but using a counter you can choose on which frames you actually want to do something. (or you can sleep the whole code making it unresponsive)
I tried the counter method, but that would clog up the whole program and still kill performance. :( I did not dare to use sleep, since I need the calls every frame as well.

Robin wrote:Well, you can use your own love.run():

Code: Select all

function love.run()
 	if love.load then love.load() end
	-- Main loop.
	local timepassed = 0
	while true do
		love.timer.step()
		timepassed = timepassed + love.timer.getDelta()
		if love.update and timepassed > 1 then timepassed = timepassed - 1 ; love.update() end
		love.graphics.clear()
		if love.draw then love.draw() end
 		-- Process events.
		for e,a,b,c in love.event.get() do
			if e == love.event_quit then return end
			love.handlers[e](a,b,c)
		end
		love.graphics.present()
	end
end
Disclaimer: untested, blah blah, own risk, blah blah... you get the idea. ;)
That worked perfectly, thanks a lot Robin. :)

Just as a general question: Why do you tend to separate Graphics and Processing in game engines? As far as I can tell, love2d on Windows only uses one thread anyways.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Slower update(dt) function?

Post by Robin »

Dangleberries wrote:Just as a general question: Why do you tend to separate Graphics and Processing in game engines? As far as I can tell, love2d on Windows only uses one thread anyways.
I suspect it's a "proper game design" thing: mixing game logic and drawing code is a Bad Thing.
Also, at least in 0.6.0, the screen is cleared between calling love.update() and love.draw(): if you move all the game logic in love.draw(), any hick-ups are going to result in screen flicker.
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Slower update(dt) function?

Post by TechnoCat »

I thought love.update was called many times before love.draw depending on your cpu? Then love.draw is called once per frame.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Slower update(dt) function?

Post by Robin »

TechnoCat wrote:I thought love.update was called many times before love.draw depending on your cpu? Then love.draw is called once per frame.
I'm not sure how it works in 0.5.0 and earlier, but in 0.6.0 every update is followed by a draw.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Slower update(dt) function?

Post by bartbes »

0.5.0 works the same way, and I expect every version before it too, it's just a loop with update called, the screen cleared, draw called and the screen updated. (basically)
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Slower update(dt) function?

Post by appleide »

Code: Select all

function love.load()
  frame = 0;
end
function love.update(dt)
  frame = frame + dt;
  if frame > 1 then
    frame = 0
     do your stuff()
  end
end
I've done this before... I'm not sure its what you described though.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 7 guests