# How accurate is "every X seconds"?

Discussion and feedback on Construct 2

### » Thu Jul 25, 2013 10:33 pm

Is this event approximate, or fairly accurate? What I mean by that is that assuming the game is running at full speed (60fps?),
1) would the timer be accurate to within miliseconds?
2) would errors accumulate? Or if the system was busy of 2X seconds, would the event fire twice in quick succession, and "catchup" afterwards?
3) If you say ran "every 1 second" say 1000 times (i.e. let the event fire 1000 times), would the elapsed time be 1000s +/- <1s?

Thanks!nutmix2013-07-25 22:34:23
B
17
S
6
G
3
Posts: 370
Reputation: 3,942

### » Thu Jul 25, 2013 11:00 pm

The lowest you could set it would be about 0.016 (or 1/60) seconds. That and anything lower would be the same as a "every tick" condition.

Back to the question as to if it's accurate: it runs if the time passed since the last time it ran is greater than or equal to the value you used.

Here's what "every 1.0 seconds" does:

global number old_time=0
+old_time + 1.0 >= time:
-- set old_time to time
-- do actions

3) It will likely be a bit lower than 1000. Of course you could test this if you wanted.
B
100
S
38
G
134
Posts: 5,555
Reputation: 85,323

### » Thu Jul 25, 2013 11:23 pm

Ha, so it will always be slightly longer than 1s, so it will accumulate a positive error over time.

I assume I need to do something different, e.g. get the timestamp of now, then check every tick if the current time stamp is >= 1s different, then 2s differnt etc. This will not have the accumulative error.

Looks like System.wallclocktime might be what i need? Im hoping this is similar to the unix timestamp in ms.
B
17
S
6
G
3
Posts: 370
Reputation: 3,942

### » Fri Jul 26, 2013 10:12 am

'Every X seconds' should not accumulate any significant error over time. If it is a tiny bit late due to only checking every 0.016 seconds, it subtracts the lateness from the next run, so the next run is a tiny bit quicker to compensate. While this introduces an (almost undetectable) amount of variation in the duration between individual runs of 'Every X seconds', it means in the long run it's actually pretty stable.
Scirra Founder
B
414
S
245
G
92
Posts: 25,198
Reputation: 200,337

### » Fri Jul 26, 2013 10:32 am

Perfect! Ill stick with that then - nice.

Thanks!
B
17
S
6
G
3
Posts: 370
Reputation: 3,942