In our game, I use a solution that relies on a global autoload class called SoundManager
, where I keep all sound effects in one place. It can be called from anywhere, and the sound will always play in full regardless of the existence of the corresponding game object.
extends Node
@onready var pickup_coin = $PickupCoin
@onready var toilet_flush = $ToiletFlush
# more sounds to add here
func play_sound(key):
var sound = get(key)
if sound is AudioStreamPlayer:
sound.play()
else:
print("Sound " + key + " not found!")
Then you can use this from anywhere:
SoundManager.play_sound("pickup_coin")
And, of course, don’t forget to add SoundManager to Project Settings > Autoload
.