Page 1 of 2

[solved]the call order between love.draw and love.update(dt)

Posted: Mon Dec 24, 2012 3:11 am
by dwdcth
I'm new to love and follow the love tutor on the wiki. Now I have a question is there a call order between love.draw() and love.update(dt).
examples,
A:

Code: Select all

function love.draw()
end
function love.update(dt)
end
B:

Code: Select all

function love.update(dt)
end
function love.draw()
end
Are there any difererces between A and B?

Re: [help]the call order between love.draw and love.update(d

Posted: Mon Dec 24, 2012 3:22 am
by BlackBulletIV
No, there isn't any difference in order between A and B. In Lua (and many other programming languages) it doesn't matter where you define the function, it just matters where you call it. In this case, love.run calls love.update and then it calls love.draw.

Re: [help]the call order between love.draw and love.update(d

Posted: Mon Dec 24, 2012 3:58 am
by dwdcth
Thank you!Your answer are helpful to me.

Re: [solved]the call order between love.draw and love.update

Posted: Mon Dec 24, 2012 5:44 am
by Jasoco
If you look at the love.run function link posted above you'll see exactly when each part is executed. You can make your own love.run if you need to. I did a few times. In my case I had moved the love.clear call to before love.update so I could still use drawing commands in love.update for debugging purposes since I could then draw useful information inside a loop and not have to run that loop again in love.draw later. (Since by default anything you draw in update gets erased before it even gets shown.)

I had also made dt a global variable so I could access it from anywhere (Since it's very useful) instead of having to pass it through every function. Someone had once said it was a pretty good idea. But it'll remain local by default. But you can make it global if you have to.

love.run does change occasionally though. When we went from 0.7 to 0.8 it changed the way sleep worked and I had thought my games were broken.

Re: [solved]the call order between love.draw and love.update

Posted: Tue Dec 25, 2012 9:11 am
by dwdcth
Jasoco wrote:If you look at the love.run function link posted above you'll see exactly when each part is executed. You can make your own love.run if you need to. I did a few times. In my case I had moved the love.clear call to before love.update so I could still use drawing commands in love.update for debugging purposes since I could then draw useful information inside a loop and not have to run that loop again in love.draw later. (Since by default anything you draw in update gets erased before it even gets shown.)

I had also made dt a global variable so I could access it from anywhere (Since it's very useful) instead of having to pass it through every function. Someone had once said it was a pretty good idea. But it'll remain local by default. But you can make it global if you have to.

love.run does change occasionally though. When we went from 0.7 to 0.8 it changed the way sleep worked and I had thought my games were broken.
Thank you ,your explanation is very helpful!
You said that you had made dt a global variabl.Do you do like this?

Code: Select all


function love.run()
...  
dt=0  --remove the "local" keyword?
...
end

Re: [solved]the call order between love.draw and love.update

Posted: Tue Dec 25, 2012 10:47 am
by bartbes
Alternatively, one could just call love.timer.getDelta().

Re: [solved]the call order between love.draw and love.update

Posted: Wed Dec 26, 2012 1:17 am
by Jasoco
bartbes wrote:Alternatively, one could just call love.timer.getDelta().
One could. But I prefer to just make it global anyway. But only if I'm bothering to use a custom love.run(). Unless there's a drawback to having dt be global at all times vs. just calling getDelta() when you need it.

Re: [solved]the call order between love.draw and love.update

Posted: Wed Dec 26, 2012 10:21 am
by Roland_Yonaba
dwdcth wrote: Thank you ,your explanation is very helpful!
You said that you had made dt a global variabl.Do you do like this?

Code: Select all


function love.run()
...  
dt=0  --remove the "local" keyword?
...
end
bartbes wrote:Alternatively, one could just call love.timer.getDelta().
Jasoco wrote:One could. But I prefer to just make it global anyway. But only if I'm bothering to use a custom love.run(). Unless there's a drawback to having dt be global at all times vs. just calling getDelta() when you need it.
Actually, When I need to access dt out of love.update, i am using a local dt var upvalue, defined in main.lua. I find it simpler (but I might be wrong). It works fine, though. I share it, for what it's worth.

Code: Select all

-- main.lua
local dt

function love.load()
  -- some code
end

function love.update(delta_time)
  dt = delta_time
  -- some code, using now dt, as usual
end

function love.draw()
  -- can access dt here, as upvalue
end

-- etc, same logic with others callbacks
Well, it also implies that dt can only be called anywhere inside main.lua. It won't be possible to access it from another chunk (an external lua file), but in general, this suits my needs.

Re: [solved]the call order between love.draw and love.update

Posted: Wed Dec 26, 2012 1:59 pm
by dwdcth
Yeah,it's a simple waw to make dt a global value.

Re: [solved]the call order between love.draw and love.update

Posted: Wed Dec 26, 2012 4:40 pm
by Roland_Yonaba
dwdcth wrote:Yeah,it's a simple waw to make dt a global value.
Well, a little precision, though. In my example, dt is not global. It is just a local variable, accessible from everywhere in main.lua. As it is defined first, all functions caming after will consider dt as an upvalue.