Delta (time) diverges from the correct time severly after only a short amount of time

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

I am currently working on a music game. The main game play focus is dodging objects. These objects should appear according to the music and are hand placed. I am currently placing these objects and recently noticed that the objects I place appear at a different time when I play from the start of the song than when starting somewhere in the middle.

I tracked this problem down to my current time measurement. At first I tried using the playback position as time indicator and defining the placement of the objects according to the playback position (which I get from the AudioStreamPlayer), but this resulted in very choppy movement, which I am also not sure why it occurred. (as fixing this would also be a fix for my problem) So I used the delta from the _process function to calculate my game time. This resulted in fluent motion of the objects, but the problem stated up top occured. After only a short time, the time I get from delta is “faster” than the one i get from the audio playback position. I then also just measured the time with my phone, and this also resulted in delta being too “fast”, and the time I get from the audio playback position to be correct.

To test, my code currently has two time variables:

  • global_time, the time used to place the obstacles, uses delta
  • time_elapsed_two, used as reference, uses the playback position

This is the code i am currently using for the time:

func _process(delta):
if(is_playing):
	global_time += delta 
	time_elapsed_two = get_node("/root/Node/Music/AudioStreamPlayer").get_playback_position()

if(Input.is_action_just_pressed("space")):
	is_playing = !is_playing
	set_is_playing(is_playing)	
pass

And a video showcasing the problem:
Video
the two timers represent the two variables, the bottom one is global_time (delta), the top one is time_elapsed_two(playback position)

How could I fix this problem? Are there any other ways to get the time? (normal Timer didn’t work, as they had the same offset as the delta, and getting the time over the current system time is, at least from what I read, problematic if you need to pause the time…)

Thank you (and sorry for the bad english)!

:bust_in_silhouette: Reply From: Xrayez

AudioStreamPlayer is running in NOTIFICATION_INTERNAL_PHYSICS_PROCESS according to source code, so perhaps you should run your code in physics_process:

func _physics_process(delta):
    if(is_playing):
    # ...

The _process() tries to run the code as fast as it can, so that most likely causes time to diverge. Perhaps it would be more correct to run this in internal physics process:

func _ready():
    set_physics_process_internal(true)

func _notification(what):
    match what:
        NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
            # update code here

If this doesn’t help, you could use this method to calculate time more deterministically:

var frame = get_tree().get_frame()
var time = frame * get_process_delta_time()
# or ...
var time = frame * get_physics_process_delta_time() # delta is fixed

Using physics update worked, thank you very much.

I actually tried this before, but I must have done something wrong…

Thank you again for the fast answer!

Tyne | 2018-08-06 19:15