Defold's sound system is weird. They do things differently with many things, which is actually annoying. Sound is one of those things where you'd expect a singleton or component to play wav, ogg, mp3, etc. Defold only supports wav and ogg.
Ok, so there is a sound component, nice. Just like Godot, you'd expect to place this somewhere load in a sound file and then play. For some reason, a sound component is expected to be pointed to a sound. Ok fine, whatever done.
Dynamically loaded sound resources works with the following code.
local soundPath = "sound.ogg";
local sfxResource = sys.load_resource(soundPath);
local soundComponentPath = go.get("#soundComponent", "sound");
resource.set_sound(soundComponentPath, sfxResource);
sound.play("#soundComponent", {some options})
I have a sound component in a "singleton" game object and it works very well. Soon as you add another sound component to that singleton and pointed towards the same sound as the previous component then the sound that is played is the last resource that was loaded in. If you play music and play sfx after, the music ends abrupty and the sfx will play. If the music component was looping, the sfx component will loop. It's like wtf is that behavior all about?
Apparently it seems that the sound component will create a buffer file (oggc, wavc) and will only use that buffer file for the rest of the lifetime. resource.set_sound is just overwriting that buffer file, so you need to have different sound files for each sound component singleton. I have short silent sound files with the name of the component.
This is one of those weird unexplained behaviors because they decided there would be one way to play sounds which is add many sound components to your game and have fun with that.
An unexplained annoyance more than anything else. People ask about these things, so might as well explain how it works so one can adjust rather than run into this thing hundred times over and have people asking again and again. Then again, Defold is niche, so not many people nor studios using it as much as Gamemaker, Godot, Unreal or Unity, so no fire lit under their asses about that.
2D Audio is implemented in some weird ass way with pan? Tf is that. If the sound component is on a game object, the expected behavior is for pan to be set by itself based on where the sound receiver is. Basically have mono over stereo with this. Use FMOD is the other approach, because that's better. It begs the question if Defold targets modern games or simple 1990s games? It's definitely lacking there for modern games.
Tisk tisk, I can see so far why King moved towards Unity (which I did buy stock in after experiencing Defold/Godot 😂 and still considering switching to). It's one thing to be barebones, its another not to have a good way to do modern things easily, making things simpler from the get go is why many games can be made faster today. UI is weird, sound is weird. Performance matters only so much before people get annoyed with the workflows. Need to get with the times.