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

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.
dwdcth
Prole
Posts: 10
Joined: Mon Dec 24, 2012 2:54 am

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

Post 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?
Last edited by dwdcth on Mon Dec 24, 2012 3:59 am, edited 1 time in total.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

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

Post 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.
dwdcth
Prole
Posts: 10
Joined: Mon Dec 24, 2012 2:54 am

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

Post by dwdcth »

Thank you!Your answer are helpful to me.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
dwdcth
Prole
Posts: 10
Joined: Mon Dec 24, 2012 2:54 am

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

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post by bartbes »

Alternatively, one could just call love.timer.getDelta().
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

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

Post 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.
dwdcth
Prole
Posts: 10
Joined: Mon Dec 24, 2012 2:54 am

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

Post by dwdcth »

Yeah,it's a simple waw to make dt a global value.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests