In this article, we will talk about one (slightly mysterious) variable in love.update(), known as dt or Delta Time.
Delta Time, also known as dt, is how much time has passed between 2 frames, hence the "delta" part of the word. Delta Time is also approximately equal to the reciprocal of the current frames per second (FPS), and consequently, FPS is approximately equal to the reciprocal of Delta Time:
dt = 1/fps fps = 1/dt
Let's look at one way to use DT.
One way to use DT is to count time, as DT multiplied by FPS is approximately equal to 1 second. You can do that by adding a global timer variable and adding the DT variable to it.
Here's an implementation of a simple timer using love.update(dt).
timer = 0 function love.update(dt) -- this adds dt every frame, adding 1 every second timer = timer + dt -- this checks if timer is greater than the time interval (in seconds) if timer >= timeInterval then -- subtract timer by interval timer = timer - timeInterval -- event end end
You can use this to fire events, for example, fire bullets every 1 second, or reload a gun every 5 seconds.
Note: Do not declare timer inside the love.update(dt) function! That will reset the timer every frame, which is not what we wanted.
Another way to use DT is to make objects move at constant speed. By constant I mean that it ideally looks the same at any framerate (I said ideally because DT isn't exact, but it's okay for most purposes). You can do that by multiplying the speed by DT. This has the effect of the object moving at that speed every second.
function love.update(dt) -- this has the effect of moving at a set speed every second, for example, 500 pixels every second object.x = object.x + speed*dt end
Another way to use DT is to add constant acceleration. Like before, by constant I mean it looks the same at any machine, slow or fast. You can do this by adding speed by acceleration multiplied by dt.
function love.update(dt) speed = speed + accel*dt object.x = object.x + speed*dt end
So, why do you want to use DT? Well, here are the 2 main reasons:
Well, that's pretty much the reasons why you would want to use DT.
Alright, I'm done with this article, I hope you learned why many lovers (pretty much every non-beginner) use DT. Any typos or suggestions? Comment here, or send me a PM in the LÖVE Forums!