Press Button > Play Sound

Godot Version 4.4.1

Question

Probably a total beginner question: I’d like to play a sound (coming from my player) when I hit a key on the keyboard. I already placed an AudioStreamPlayer2D on my Player node and assigned a key-bind in the Project Settings but I have no clue how to go about code-wise.

I tried setting up a function with an if-statement and action_is_pressed(“yolo”) but all I got was error messages. It seems pretty simple on paper.

Could anybody point me in some direction that is helpful, please?

Thanks for your time!

You can use a function to hear the input events. In there you can add the if statement for the action you defined and there play the sound. Something like this:

 func _input(event: InputEvent) -> void:
 	if event.is_action_pressed("ui_accept"):
 		$AudioStreamPlayer.play()
1 Like

You sir, are a genius! Thanks a ton!

2 Likes