The title pretty much sums up what I’m trying to figure out.
I have a game where multiple instances of an enemy are spawned in at the same time. I would like for there to be a sound that this enemy makes simply by existing, and the sound will keep looping until the enemy no longer exists (is destroyed). The “play sound” action doesn’t seem to work because once you loop a sound with that action, there’s no way to stop that individual sound. So I tried to “Play a sound on a channel”, which allows me to start and stop individual sounds, but I have to assign a channel to each sound, which I tried doing with some clever uses of an incrementing global variable, saving the channel to the individual instance, then recalling the channel to be stopped when the object was destroyed, but this turned into a big mess and didn’t work all that well.
Not sure if there is a better solution, but I would do it with a structure variable, storing as children the available channels. You can check if there is an available channel (child exists), use this channel for an enemy (delete child), and when you delete the enemy enable the channel again (recreate the child).
The structure would be like (the child value doesn’t matter, the name only):
Now in a Repeat loop we can search for an available channel:
[code]Conditions: Conditions to spawn an Enemy
Actions: Create object Enemy
Do = 0 to variable “index”
Repeat 10 times:
Conditions: Variable "Channels" has child ToString(Variable(index))
Actions: Do = Variable(index) to variable "channel" of Enemy
Delete child ToString(Variable(index)) from variable "Channels"
Do = 999 to variable "index"
Conditions: No conditions
Actions: Do + 1 to variable "index"[/code]
(The 999 is to make sure that after a channel is found and the child removed, no more channels are found when the loop continues, because there is no channel 999 or above).
Now each Enemy can play sounds on the channel
Finally, when you delete the Enemy open the channel again:
Conditions: Conditions to delete an Enemy
Actions: Do = 0 to variable Channels[ToString(Enemy.Variable(channel))]
If you have more than 10 (or number of channels) enemies, some of them won’t have a channel, I guess they’ll use the channel 0 by default, you can change that giving default “channel” variables values of course.