Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

timer.performWithDelay inaccurate time with cooldown timer
Started by nml2727 Nov 01 2019 08:33 AM

- - - - -
4 replies to this topic
timer performwithdelay timer.performwithdelay system.gettimer time cooldown cooldown timer

Best Answer nick_sherman , 01 November 2019 - 09:53 AM

Frame delta times aren't consistent. Ideally they would always be 16.66ms apart but it will generally be somewhere north or south of that - if you Google there have been a number of discussions on here about that.

So I guess you've got to adjust the number of slices to remove based on the time elapsed since the last frame compared to the exact 1/60th.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 89 posts
  • Corona SDK

I have a cooldown timer which I made by creating a small slice and displaying it 360 times to make a circle.  

 

To show the cooldown is being reset, I remove a slice of the circle.  

 

It works fine, except that the time I am expecting it to take to run the code to remove one slice is taking longer than expected, and is not consistent...  

 

Here is my block of code to remove the slice:

local the_time = system.getTimer()
local slice_counter = 1

local function removeSlice()
  print("time since last ran: " .. (system.getTimer() - the_time))
  the_time = system.getTimer()

  display.remove(cd_slices[slice_counter])
  cd_slice[slice_counter] = nil
  if(slice_counter == 360) then
    cdOver()
  end
  slice_counter = slice_counter + 1

end

print("cd_length: " .. cd_length)
cd_timer = timer.performWithDelay(cd_length, removeSlice, 360)

I would expect the time taken between each time it removes a slice to be the same, and to be equal to cd_length. 

 

But it is not.... 

 

cd_length is 5.6, but

 

time since last ran is all over the place, ranging from  9 to 21 instead of 5.6.  

 

Am I doing something wrong or misunderstanding how timer.performWithDelay works??



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,920 posts
  • Corona SDK

By default, timed functions are only triggered on a frame tick, so the only granularity you can get is 30 or 60 fps. 

 

However, they did recently add an undocumented flag that you can set - timer.allowIterationsWithinFrame = true

 

I've not used this personally so whether it will do what you want I'm not sure. However, whether you can fire events between frames or not, you can't update any of the visuals any faster than 30/60 fps. You'll either have to have your cool down timer take exactly 360 frames, or remove more than one slice per frame.



[TOPIC: post.html]
#3

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 89 posts
  • Corona SDK

So I changed the code to remove 10 slices instead of just 1.  So in theory I would need the cooldown to be at least 0.6 seconds right?

 

360 slices / 10 means it needs 36 iterations.  Game is set to 60 fps so  36/60 gives me 0.6 seconds.

 

I set my cooldown to be 2 seconds(which should be plenty of time) so I end up with cooldown_length of 54.  

 

However, I still see the time taking longer than 54.  It ranges from taking 55 to 74.  

 

Did I miscalculate something there?



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,920 posts
  • Corona SDK

  Best Answer

Frame delta times aren't consistent. Ideally they would always be 16.66ms apart but it will generally be somewhere north or south of that - if you Google there have been a number of discussions on here about that.

So I guess you've got to adjust the number of slices to remove based on the time elapsed since the last frame compared to the exact 1/60th.

[TOPIC: post.html]
#5

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 89 posts
  • Corona SDK

Gotcha.   It makes sense what is happening now.  Thank you!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: timer, performwithdelay, timer.performwithdelay, system.gettimer, time, cooldown, cooldown timer