Page 1 of 1
SDL_GetTicks equivalent in love?
Posted: Wed Oct 24, 2018 4:29 am
by love2d
I see this:
https://love2d.org/wiki/love.timer.getTime
So I could technically just start a timer and never finish it.
Basically need a timer that returns some fraction of a second identical to SDL_GetTicks.
Also a problem SDL_GetTicks has, is it overflows at 49 days, but you can get around that using modular arithmetic such as ""end - start >= interval". Is this issue the same in love2d?
Re: SDL_GetTicks equivalent in love?
Posted: Wed Oct 24, 2018 5:12 am
by grump
Not sure what you mean by "start a timer". LÖVE only has global time, no timers that can be started or stopped.
getTime returns the time in seconds that has elapsed since some unspecified starting time. It should do what you ask for. You can also keep track of time in love.update(dt), where dt is the time in seconds since the last frame.
The overflow after 49.71 days happens because a 32 bit unsigned integer counting milliseconds will overflow after that time. LÖVE does not use that, and AFAIK overflows will not happen (within a reasonable time frame).
Re: SDL_GetTicks equivalent in love?
Posted: Wed Oct 24, 2018 5:17 am
by love2d
grump wrote: ↑Wed Oct 24, 2018 5:12 am
Not sure what you mean by "start a timer". LÖVE only has global time, no timers that can be started or stopped.
getTime returns the time in seconds that has elapsed since some unspecified starting time. It should do what you ask for. You can also keep track of time in love.update(dt), where dt is the time in seconds since the last frame.
The overflow after 49.71 days happens because a 32 bit unsigned integer counting milliseconds will overflow after that time. LÖVE does not use that, and AFAIK overflows will not happen (within a reasonable time frame).
> since some unspecified starting time
Can you give more context about this?
dt doesn't seem to accurate, but I could add that to a variable to get a semblance of getTicks
getTime returns in seconds, but I would need in milliseconds or something much smaller. GetTicks returns a small enough number that can do color shifting or animations using it.
Re: SDL_GetTicks equivalent in love?
Posted: Wed Oct 24, 2018 6:09 am
by grump
love2d wrote: ↑Wed Oct 24, 2018 5:17 am
Can you give more context about this?
It has an unspecified value when you start your game, it doesn't start at 0.
getTime returns in seconds, but I would need in milliseconds or something much smaller. GetTicks returns a small enough number that can do color shifting or animations using it.
It returns seconds, but that is not the resolution of the timer, which is much higher (down to microseconds). Just multiply with 1000 to get milliseconds. Note that you don't work with ints in Lua, all numbers are 64 bit floating point values.
Re: SDL_GetTicks equivalent in love?
Posted: Wed Oct 24, 2018 7:30 pm
by zorg
If you do need an SDL_GetTicks like function then here's one way to do it:
Code: Select all
-- Put this in love.load (or at the top of main.lua or whatever)
initTimeOffset = love.timer.getTime()
function getTicks()
-- accurate exactly to milliseconds.
return math.floor((love.timer.getTime() - initTimeOffset) * 1000)
end
But again, i don't know the internals of the clock being used, so this may also suffer from the occasional (but very rare) overflow issue you mentioned;
Then again, if you want to play timekeeping on the scale of months, i'd suggest completely separating the date portion from the time portion altogether; it's more sane to do so anyway.