Page 1 of 2

The use of dt

Posted: Sat Feb 28, 2015 8:48 pm
by Bindie
Hey, I know dt, delta-time. It will sync the game to the same speed in different computers. Is this a variable I have to declare? I have seen people not declaring it, in tutorials, is it built in?

Re: The use of dt

Posted: Sat Feb 28, 2015 8:59 pm
by Muris
Update-function passes variable that is time in seconds from last update in other words:

Code: Select all

love.update(dt)
   print(dt) -- dt can be used in here, it is time difference from the last update
end

Re: The use of dt

Posted: Sat Feb 28, 2015 9:07 pm
by Bindie
Can one use it in love.draw?

Re: The use of dt

Posted: Sat Feb 28, 2015 9:41 pm
by s-ol
Bindie wrote:Can one use it in love.draw?
No, it's a parameter to love.update, therefore it's local to that function's scope. Also you shouldn't use it in love.draw for semantic reasons anyway: love.draw only ever draws the current state, it doesn't modify anything do you don't need dt.

And if you don't know what a parameter is or where to look for one, you should learn Lua before you start a löve projecty, PIL is a very good resource for that.

Re: The use of dt

Posted: Sat Feb 28, 2015 10:25 pm
by Robin
If you merely want to draw the dt or other performance related stats like framerate, you can use these functions: [wiki]love.timer.getFPS[/wiki], [wiki]love.timer.getDelta[/wiki] (<= this one is the same as the dt passed to love.update) and [wiki]love.timer.getAverageDelta[/wiki].

Re: The use of dt

Posted: Sat Feb 28, 2015 10:53 pm
by Bindie
I only needed to know why dt could be used and only in love.update, I know what a parameter is. Be gentle.

Re: The use of dt

Posted: Sat Feb 28, 2015 11:01 pm
by s-ol
Bindie wrote:I only needed to know why dt could be used and only in love.update, I know what a parameter is. Be gentle.
I didn't mean to be rude; We just happen to have a lot of very unexperienced programmers here and I try to anticipate that when I answer a question.

Re: The use of dt

Posted: Sat Feb 28, 2015 11:09 pm
by Bindie
Saw it from the wrong perspective, peace.

Re: The use of dt

Posted: Mon Mar 02, 2015 10:30 pm
by Bindie
Hey, I haven't implemented dt to every equation yet. If I think I got a perfect timing for example a laser timer, without dt as a factor, do I have to recalibrate that perfect timing when I implement dt?

Re: The use of dt

Posted: Tue Mar 03, 2015 1:47 am
by Clavus
Bindie wrote:Hey, I haven't implemented dt to every equation yet. If I think I got a perfect timing for example a laser timer, without dt as a factor, do I have to recalibrate that perfect timing when I implement dt?
Don't quite get what you're saying here. Point is, the dt variable just indicates how much time has passed since the last time love.update was called. Depending on your game's performance (which depends on how much you did in that last love.update call and your computer's available CPU power), this number can vary.

Say your game only calls love.update about 50 times per second. Then, on average, dt will have the value of about 0.02 (as in 1/50). If you have, for example, a spaceship that you want to move 200 pixels to the right per second, you'd do it something like this:

Code: Select all

local spaceship_image -- the image of your spaceship you loaded in
local spaceship_x, spaceship_y -- the x and y position

function love.update( dt )
    spaceship_x = spaceship_x + 200 * dt
end

function love.draw()
    love.graphics.draw(spaceship_image, spaceship_x, spaceship_y)
end
Now if love.update is run 50 times per second, where dt around 0.02, then spaceship_x is incremented with 200 * 0.02 = 4 pixels every love.update call. Note that the value of dt can vary, but spaceship_x will always have incremented by 200 pixels after every second.

And as you can see, you only really call drawing functions in love.draw. You do not update the 'state' of your game there, like object positions and such.