one second timer

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

one second timer

Post by lolsasory »

how would i say every 1 second add 1 to x?
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: one second timer

Post by Jasoco »

There are a few timer libraries out there. Look up Cron I believe. Or Hump which has a Timer part.

Cron: http://love2d.org/forums/viewtopic.php?f=5&t=2882
Hump: http://love2d.org/forums/viewtopic.php?f=5&t=1795

With Cron you'd use the cron.every to create a simple function that would perform your addition task every second.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: one second timer

Post by Plu »

If you want a basic function, use two variables like this. (It goes in love.update)
One of them is integer based and the other is a float that collects pieces of passing time from the dt that love.update() gets. Then every time more than a second has been collected, that seconds gets moved to the other function.

(You'll have to define baseTimer and seconds, they need to start at 0 )

Code: Select all

baseTimer = baseTimer + dt
if baseTimer > 1 then
  baseTimer = baseTimer - 1
  seconds = seconds + 1
end
Alternatively, if it's just about the number, you can also just count time as a float and then use its rounded-down version:

Code: Select all

baseTimer = baseTimer + dt
-- now you can use math.floor( baseTimer ) to get the number of seconds as an integer
Post Reply

Who is online

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