I'm trying to put a sound at the end of my level in order to signal the level has ended when the enter button is pressed

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

I have this code, where I have embedded the sound to play when the button is pressed which also connected to the scene transition. Therefore, the order of my transition would be Enter button pressed, and then the sound played, and then the transition.

func _input(event):
if event.is_action_pressed(“NextLevelEat”):
$NextLevelSound.play()
if get_overlapping_bodies().size() > 0:
visible = false
change_to_next_level()

This is the code I have, but when the scene is played the sound is very short instead of the length of the audio it should be.

:bust_in_silhouette: Reply From: Ertain

The scene changes before the sound has finished playing. Try making the script wait until the sound finishes playing. (Note: I don’t know if this is how the code is structured.)

func input(event):
    if event.is_action_pressed("NextLevelEat"):
       $NextLevelSound.play()
        # Stop the script until the sound has completed playing.
        yield($NextLevelSound, "finished")
   if get_overlapping_bodies().size() > 0:
       visible = false
   change_to_next_level()

Please note that the “finished” signal won’t be emitted if the sound loops.

:bust_in_silhouette: Reply From: Gluon

You are not checking if the sound has finished before you change the level I suspect that is the issue.

you can use

if $newtlevelsound.is_playing() == false:

before you run the change to next level that should work although without seeing the rest of your code its difficult to say for sure.