The use of 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.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

The use of dt

Post 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?
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: The use of dt

Post 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
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: The use of dt

Post by Bindie »

Can one use it in love.draw?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: The use of dt

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: The use of dt

Post 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].
Help us help you: attach a .love.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: The use of dt

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: The use of dt

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: The use of dt

Post by Bindie »

Saw it from the wrong perspective, peace.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: The use of dt

Post 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?
User avatar
Clavus
Prole
Posts: 28
Joined: Wed Jan 27, 2010 12:45 pm

Re: The use of dt

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

Who is online

Users browsing this forum: No registered users and 1 guest