Synchronizing music and animation

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

Question: how can I synchronize music and a piece of animation?

I have a game where the music and animation must be perfectly synchronized (or at least, almost perfectly). This is for a game where tapping to the beat is one of the main mechanics.

I have a script which starts the two pieces of media in the _ready() function, and monitor them in the _process() function. While this works just fine on my desktop computer, it doesn’t work so well on my laptop. I’m not entirely sure why it’s slower on my laptop. It could be due to the music loading (it’s an OGG file).

Here’s a sample of my striped down code.

extends Node2D

onready var music_stream = $AudioStreamPlayer
onready var animation_stream = $AnimationPlayer

func _ready():
    music_stream.play()
    animation_stream.play("walking")
    
func _process(delta):
    if music_stream.get_playback_position > 2.0 and Input.is_action_pressed("ui_accept"):
        load_next_scene()

I’m using Godot version 3.0.3.

:bust_in_silhouette: Reply From: ZacB

I’m pretty sure it’s due to the different delta times between your two PCs. So maybe you can use an event (signal) triggered when the player presses a specific input.

Thank you for the response, ZacB. The thing is, the animation and music are almost independent from the user input. Rather, the user responds to the music and animation, and then some other event happens.

Ertain | 2018-07-08 13:45

Like a PianoTiles game or Guitar Hero ?

ZacB | 2018-07-09 16:38

Yes, something like that.

Ertain | 2018-07-09 20:07

I think u can use a custom event.

ZacB | 2018-07-10 05:51

That’s what I eventually ended up doing. It took me a couple of tries, but I eventually settled on a custom countdown timer scene which fired off a signal that, in turn, executed a function in the main node’s script. That starts off the mini game, and keeps both the animation and music in synchronization with each other.

Ertain | 2018-07-11 19:41