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.
Slower update(dt) function?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Slower update(dt) function?
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)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Slower update(dt) function?
Well, you can use your own love.run():
Disclaimer: untested, blah blah, own risk, blah blah... you get the idea.
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
Help us help you: attach a .love.
- Dangleberries
- Prole
- Posts: 3
- Joined: Fri Jan 01, 2010 11:22 am
Re: Slower update(dt) function?
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.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)
That worked perfectly, thanks a lot Robin.Robin wrote:Well, you can use your own love.run():
Disclaimer: untested, blah blah, own risk, blah blah... you get the idea.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
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.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Slower update(dt) function?
I suspect it's a "proper game design" thing: mixing game logic and drawing code is a Bad Thing.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.
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.
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Slower update(dt) function?
I thought love.update was called many times before love.draw depending on your cpu? Then love.draw is called once per frame.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Slower update(dt) function?
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.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.
Help us help you: attach a .love.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Slower update(dt) function?
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?
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
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 3 guests