I'm new to Godot and I'm trying to play an explosion sound effect when deleting an object.
Basically when bullet is in collision with object, play sound effect and delete object.
The problem is that the AudioStreamPlayer is a child of the object I want to delete and because I delete the object on collision, the sound never play. I was thinking about to put the AudioStreamPlayer higher in the hierarchy and then send a signal to play it, but the problem with that is I have many instances of this object in the scene and I may delete multiple in short amount of time and so I need to play multiple instances of the sound but Godot seem to stop and start over the same sound when call the .Play() method of AudioStreamPlayer.
So I have two solutions in mind but I dislike both.
- Send a signal to the root node higher in hierarchy when object is about to exit, create an instance of the AudioStreamPlayer higher in hierarchy, play it, when finished, delete AudioStreamPlayer instance.
- instead of deleting the object on collision I play the sound and delete the object after the sound is finished.
The reason I don't like the first option because it is creating a mess in the project and also don't like the idea to constantly create and delete instances of sound object. And the reason I don't like the second option is that I still need to get rid of the object as soon the object colliding with the bullet so then what I would also need to do is play the sound, hide the object, disable collision mask so the player and everything stop interacting with it and then delete it when the sound finished.
Is there any better way to play multiple instances of a sound without need to go trough so much trouble or to play a sound when we delete an object?
In most game engines there is normally two audio objects to use one for Music and one for Sound. The difference is that a Music object can play only 1 instance of audio at the time and the Sound object can play multiple instances out of the box, no work around required. I can't find anything like this implemented in Godot but AudioStreamPlayer which seems to allow to play only 1 instance of the audio at the time. I need to create instances of the AudioStreamPlayer in order to solve this.
I would appreciate any help.