Page 1 of 1

How to make a timer ? [Answered]

Posted: Wed Jan 25, 2023 1:26 pm
by zalander
I am new to love2d and I was making a simple clicker game and needed a timer for that so I made a variable in the game table named "t" and placed it in the update function:
function love.update(dt)
game.t = game.t + 1
end
but I realized that the t variable will be added with 1 with the frame rate so on fast computers the timer will run faster so how can I fix that ?
I hope you guys will help me fix this :nyu:

Re: How to make a timer ?

Posted: Wed Jan 25, 2023 2:58 pm
by gcmartijn
https://github.com/rxi/tick ?

https://github.com/love2d-community/awesome-love2d
Search for the keyword "timer"

Maybe you find something that works

Re: How to make a timer ?

Posted: Wed Jan 25, 2023 3:03 pm
by MrFariator
The simplest solution to your timer issue is incrementing by delta time, instead of by a fixed value:

Code: Select all

function love.update(dt)
  game.t = game.t + dt
end
Of course, because this involves floating points, you can't check for exact equivalence ("game.t == 101"), and instead need to use greater/less than operators (<, >, <=, >=). Another issue is that if the delta time between frames suddenly jumps up or down, you might miss any timer "ranges" you were seeking to capture (eq. you're waiting for timer to be between 100 and 105. However, a lag spike causes the timer to jump from 98 to 107), requiring extra care.

If you'd like to use integer timers, then you'd have to implement something like fixed timestep in your code. This library can do it for you.

Re: How to make a timer ?

Posted: Wed Jan 25, 2023 4:15 pm
by zalander
Thanks for the help !
Its fixed now :awesome: