Hullo~
So I’m trying to write some code in order to stop other music and then start playing some new musics. I’m wondering if there’s any way to get an entire mixer group to just stop in Defold.
if music ~= "25" then
sound.stop() --stop all sounds (music) from a mixer group here
sound.play("/music#song")
music = "25"
end
…You know, because trying to individually stop every single sound by writing every single url (especially if you’re going to have 100+ maybe songs total in your OST) would be tedious and not fun.
Do you know about loops? If all of your songs are called “song1”, “song2” etc, you can do this:
self.songs = {} --this creates a table where we will store some addresses
for i = 1, 25 do --do this 25 times, where "i" changes for the numbers 1 to 25
self.song[i] = "/music#song"..i --stores each song url in the table
end
then, to stop the music, do this:
for i = 1, 25 do
if i == self.music do
sound.play(self.songs[i])
else
sound.stop(self.songs[i])
end
end
When you do sound.play() you can save the id that you are playing at the same time somewhere.
You could save this in a central places like a Lua module. A basic example of a Lua module is like
local M = {}
return M
Then this file can be required in scripts to share data.
local my_module = require("imports.my_module")
...
my_module.current_song = id
...
sound.stop(my_module.current_song)
You can also save tables of currently playing sounds to know what to stop too.
I recommend keeping your sounds all in one place, such as in the main collection, and then having a central place that does the actual sound.play() etc instead of having that everywhere.
How would I do that?
If any of this is not clear yet please be specific on which part you have a question on.
Besides using builtin Defold audio you can use FMOD too which has a bit more existing features related to this.