Timing issue for 1 second interval (or any interval it seems).

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jitterbeetle

Since we run in a loop at 60Hz, or 60FPS if you like, how do I do something at 1 second intervals?
(don’t know how to format code here)

  
 var globalsleep = 1.0 - (1/60)
 var prevdelta = 0.0
    
        func _ready(): 
        # get ready now
    
    func _process(delta)
    prevdelta += delta
        if prevdelta >= globalsleep:
    print("prevdelta = ",prevdelta)
    prevdelta = 0.0

But there is always some time lost and prevdelta will be more than 1 second.

But that is strange because if I run and just print(" delta = ", delta) I see 1/60 or 0.0167

Something seems broken to me. I am using git master.

I hope someone can try to explain this but I think it’s a bug. Or is there some kind
of global lock like python GIL? Which would mean that Godot is all in a single-thread.

Or am I misunderstanding some signficant design behavior?

Might not be what you’re looking for, but you maybe could use a timer node set to 1 second. Not sure if that is more precise.

Diet Estus | 2018-05-16 14:13

I will look at that but unless it uses some other mechanism the timer node would be coupled to the same timing mechanism and suffer the same symptoms.

There were some timing related commits recently so I need to pull, rebuild and retry.

jitterbeetle | 2018-05-16 15:46

:bust_in_silhouette: Reply From: camarones

I imagine that there is some sort of rounding error going on. 1/60 is not exactly 0.0167, but a bit less: 0.01666666666666666666666… Therefore if you are adding 0.0167 every step, you will end up with 1.002, even if there are exactly 60 steps per second. If it is important that you arrive at 1.000 exactly, subtract the 2 milliseconds every 60th frame.

No. I can set globalsleep = 1.0 - (2/60) and makes no difference. It’s easy to try it out and see it.

jitterbeetle | 2018-05-16 15:43