Page 1 of 1
Slower update(dt) function?
Posted: Fri Jan 01, 2010 11:28 am
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.
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 11:55 am
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)
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 12:28 pm
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.
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 1:36 pm
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.
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 1:48 pm
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.
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 4:40 pm
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.
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 4:43 pm
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.
Re: Slower update(dt) function?
Posted: Fri Jan 01, 2010 5:11 pm
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)
Re: Slower update(dt) function?
Posted: Sun Jan 03, 2010 12:03 am
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.