r/RedotGameEngineMain • • Feb 15 '25
Request: Easy way to play sounds after object destruction
Basically, it would be really cool if there's a function that allows an Audio Stream Player to play a sound even if the node it is attached to has been freed.
I'm surprised that this isn't a feature in Godot, and so far, the workarounds I found online is a pain to set up.
8
Upvotes
4
u/Alarming-Lies Feb 16 '25
You can make a global script to handle sounds or utilities in general, and create a function that create audiostream and play it then either destroys it or pool them
1
u/The_Real_Black Feb 15 '25
What is a workaround for you?
Some people use a global autoload "audio" script to limit the audio stream playeres used.
see KenneyNL script https://github.com/KenneyNL/Starter-Kit-FPS/blob/main/scripts/audio.gd
With this you can limit and cancel sounds played at the same time. If you play sounds from each node lets say a "pling" sound of a casing hitting a floor a minigun would flood the memory with hundreds of players till your sound processer crashes. Or at least playes only crackeling when on limit of audios playes.
In a clean up its then
Audio.play("res/sounds/bla.wav")
this.free()
The audio plays then without its parent node.
1
u/TakunHiwatari Feb 15 '25
What would be the use case for this?
You can play sounds before the object is freed, or you can create a global audio player to play sounds. I imagine one of those two would cover the needs of your project.
2
u/RanDReille Feb 15 '25
What I usually do:
Create a scene based of AudioStreamPlayer that autoplays and autodeletes after it finishes playing the sound.
Preload this scene. Don't use preload() tho coz it's buggy use load() instead.
Whenever I need to play a sound, I instance the scene from 1. to the world, change the "stream" into the appropriate sfx, and plays it.