Advice on how to manage my audio players

Godot Version

4.2.1.rc1

Question

I’m building a shmup game and it is possible to collect “fragments” when destroying an enemy’s ship. When collecting them, an audio effect is played. My question is where do I place my audio player? Because if I add it to the fragment, and collect a lot in one time, the sound becomes glitchy.

I would like to ask the same question about UI buttons. I have placed the audio player within the button, and it plays a sound when focused or pressed, but there is no trouble related as it’s very hard to play buttons effect in the same time. Is it a good practice to place the audio player node within the button itself? If not, how should I approach this?

Thank you very much for your advice on this best practice!

1 Like

The solution is to have an AudioManager autoload that can queue sounds. There is a good tutorial on KidsCanCode here

1 Like

the sound becomes glitchy.

This is because an AudioStreamPlayer node can only play a single AudioStream at a time. If you call play again while it is already playing, it will sound like the first has “cut-out” as it plays again from the beginning.

where do I place my audio player?

One solution to this problem is to create an AudioStreamPlayer dynamically per sound effect and add it to the scene using add_child.

func play_sfx(audio_stream: AudioStream) -> void:
	var player: AudioStreamPlayer = AudioStreamPlayer.new()
	add_child(player)
	player.finished.connect(player.queue_free)
	player.stream = audio_stream
	player.bus = "Sound_Effect"
	player.play()

Now whenever you want to create a sound effect, you could call play_sfx with the appropriate AudioStream. This function could be part of an autoload such that it can be called from any node.

Thank you for your answer, GBWD.

That is exactly my issue. Maybe I didn’t explain it well, and I’m sorry for this.

My AudioPlayer is attached to the fragment, which means I have 10 audio players if I have 10 fragments. If I collect them at the same time, all 10 audio players will play the effect simultaneously, resulting in glitchy sound.
This is what I would like to avoid. :slight_smile:

Thank you for your answer, FreakyGoose.

Yes, it is a solution. The slight difference is that I don’t need a queue.

Imagine a Mario game where you collect 10 mushrooms; the sound effect when Mario becomes big is played once. That is precisely what I want to achieve, but I don’t know the best practice about this.

Before playing the sound you could check if one is already playing ( is_playing bool on the AudioStreamPlayer), then just never start it if one is already going. Something like this

if AudioStreamPlayer.is_playing == false:
   play sound code
else:
   pass

Might also work with an array or group of the AudioStreamPlayer nodes if you want a certain number to be able to play.

2 Likes

Yeah, I will opt for this solution. Using a queue seems out of my needs.

Thanks everyone for your help and time!

PS: @jking, the method is AudioStreamPlayer.is_playing() actually :wink:

2 Likes

How do you put the sound in it though

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.