How to play a single instance of an audio and mute others, when there are multiple instances of that audiostreamplayer2D in the scene?

Godot Version

4.2.2

Question

In my game, I currently have a scene named “PlayerGrenade”.
The node tree is as follows.

As the screenshot shows, there are currently two AudioStreamPlayer2D’s in this scene.

  • One for the countdown beeping, (Has a set duration. The Audio player stops playing when an enemy steps on the grenade hitbox.)
  • And the other for when it explodes. (Plays after either the grenade countdown ends or if an enemy steps on the grenade hitbox. )

In the game, after pressing the relevant button, the player throws 3 grenades at once. (This is three instances of the PlayerGrenade scene.)

Therefore, this will create 3 of each ASP. (AudioStreamPlayer)
Which is quite loud.

My requirement is to only play one of each ASP when the 3 grenades are instantiated into the Game Level scene. (One Grenade beep ASP and one Grenade Detonation ASP.)
And if one grenade explodes before the complete countdown time has passed, the next ASP should take over and play the remainder of the sound effect.

I’ve thought of using an ASP as a singleton and calling it when needed. But I’m having a bit of a difficult time implementing it properly.

Any assistance on this would be much appreciated. Whether with implementing this via Singletons or any other method.

Thanks in Advance!

The problem with using a Singleton, is only one can exist at a time. An easier way would be to:

  1. Create a Grenade audio bus and set max_polyphony to
  2. Then all your grenades use it. But only one at a time will actually be able to play.
  3. Then you create an explode signal when a grenade explodes that you emit. It should be emitted when the explosion sound or animation finishes. (There are signals you can tie into for this.)
  4. Then you connect the signal in the ready function for the grenade instance so that when it is emitted, you call the countdown timer.
  5. When the last grenade calls this, there will be no other grenade to pick it up and nothing will happen.