How to make a timer ? [Answered]

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.
Post Reply
User avatar
zalander
Citizen
Posts: 72
Joined: Mon Jan 09, 2023 5:58 am
Location: India

How to make a timer ? [Answered]

Post 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:
Last edited by zalander on Wed Jan 25, 2023 4:16 pm, edited 1 time in total.
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
gcmartijn
Party member
Posts: 134
Joined: Sat Dec 28, 2019 6:35 pm

Re: How to make a timer ?

Post 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
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: How to make a timer ?

Post 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.
User avatar
zalander
Citizen
Posts: 72
Joined: Mon Jan 09, 2023 5:58 am
Location: India

Re: How to make a timer ?

Post by zalander »

Thanks for the help !
Its fixed now :awesome:
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 16 guests