Page 1 of 1
Calculation inside love.draw?
Posted: Wed Aug 29, 2012 2:28 am
by chute
Hi all.
First off, I apologize, as I'm sure I've seen the answer to this before, I just don't remember where, what the answer is, or what to search for.
Besides dt in love.update and drawing functions in love.draw, is there any real difference between love.update and love.draw?
can I just use:
Code: Select all
function love.update(dt)
delta = dt
end
and do all of the rest of my coding in love.draw?
Thanks!
Re: Calculation inside love.draw?
Posted: Wed Aug 29, 2012 3:22 am
by dreadkillz
In the default love.run, love.draw gets called after love.update. There's no difference if you put everything in love.draw or love.update except you can't draw stuff outside of love.draw. You can pass dt to love.draw if you want. It's good practice though, I believe, to separate your game logic from your drawing operations.
https://love2d.org/wiki/love.run
No love.update:
Code: Select all
function love.run()
if love.load then love.load(arg) end
local dt = 0
-- Main loop time.
while true do
if love.timer then
love.timer.step()
dt = love.timer.getDelta()
end
if love.graphics then
love.graphics.clear()
if love.draw then love.draw(dt) end -- pass dt to love.draw
end
-- Process events.
if love.event then
for e,a,b,c in love.event.poll() do
if e == "q" then
if not love.quit or not love.quit() then
if love.audio then
love.audio.stop()
end
return
end
end
love.handlers[e](a,b,c)
end
end
if love.timer then love.timer.sleep(1) end
if love.graphics then love.graphics.present() end
end
end
Re: Calculation inside love.draw?
Posted: Wed Aug 29, 2012 3:34 am
by Ensayia
Technically there isn't anything stopping you from calling 'calculation' in love.draw() however by default love.draw does not have access to delta time (dt). THe developers have split game logic and drawing to separate callbacks to keep things neat.
It's not very good practice to use love.draw() as an update loop.
EDIT: Also, unless you have a very good specific reason there's no need to modify love.run().
Re: Calculation inside love.draw?
Posted: Wed Aug 29, 2012 4:35 am
by Inny
The reason that dt is given to love.update is because you're going to use it for moving sprites, and things like AI and collision detection use sprite locations.
Personally, I prefer making dt available for love.draw, so that animations can be done there. But, I stay with the spirit of the variable and pass it as a parameter to functions, rather than accessing a global variable. If I should need to not-animate, for some reason, I can pass a dt of 0.
Re: Calculation inside love.draw?
Posted: Wed Aug 29, 2012 6:09 am
by Ensayia
Just an FYI, you can call love.timer.getDelta() anywhere. This is how love.run() assigns it to dt.
Re: Calculation inside love.draw?
Posted: Wed Aug 29, 2012 12:44 pm
by Lafolie
Ensayia wrote:Just an FYI, you can call love.timer.getDelta() anywhere. This is how love.run() assigns it to dt.
Since things are designed so that little-to-no calculations are performed in the draw step, you shouldn't need to use getDelta very often since you can just pass/expose dt to your object/function. If you're referencing it a lot I guess you could trim a few calls by using dt instead of manually getting it from getDelta. References are cool.
Re: Calculation inside love.draw?
Posted: Wed Aug 29, 2012 1:19 pm
by chute
Thanks all for your help!