0 votes

Hello,
I am new to Godot and I want an object to play a simple sound before disappearing. So I added an AudioStreamPlayer node to my object, dragged and dropped a wav file to the Stream box. I wrote the following code but it doesn't play. Any ideas?

func _on_block_brk_1_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
#print("Any input event")
if event is InputEventMouseButton:
    if event.is_pressed():
        print("Object clicked")
        $AudioStreamPlayer.play()
        queue_free()
asked Apr 11 in Engine by korayozbay (18 points)

1 Answer

+2 votes

$AudioStreamPlayer.play() will start playing the sound, but won't wait until it's finished. So you start playing the sound and then immediately delete the object.

This shoukld work:

$AudioStreamPlayer.play()
yield($AudioStreamPlayer, "finished")
queue_free()
answered Apr 11 by njamster (8,874 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.