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!
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.
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.
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.
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.