How to check if sound is playing in a component?

Is there a way to check if a sound from a component is playing? For instance

-- Pseudo code, toggle music
-- #bgmusic is a sound component
if sound.is_playing("#bgmusic") then
   sound.stop("#bgmusic")
else
   sound.play("#bgmusic")
end

of course, sound.is_playing() doesn’t exist but is there something I could use to get if a sound is playing?

You could make a basic system to track this. When playing, set a flag to true. When receiving the sound_done message, set it to false.

That sounds like a bit of a workaround. Any other external script would have to know this special script that tracks the sound playing. You’d also have to use this special script to play the sound otherwise the flags wont be properly triggered. It sounds like a bit of can of worms, prone to error…

I think it depends on how you design it. I have one sfx script and module that handles this across the project. Instead of calling sound.play() I call for example sfx_module.play_sound() which handles any administration and then plays the actual sound.

1 Like

I would also use the same strategy as Alex, having one “controller” script that can be easily maintained, as opposed to sprinkling sound.play() calls throughout the code.

The reason why there is no similar function built into the engine, is that it’s easy to keep track of yourself. And also, not all users need such functionality.
In such cases we simply opt out of putting the function into the engine.
It is a core design philosophy, and a major reason why the engine stays so small.

5 Likes

We come back to this recommendation over and over. I think it might be worth creating an example for this?

2 Likes