here is the full solution I used:
Code: Select all
samplechannelspriority = {}
For Local n = 1 To 8
samplechannelspriority[n] = 0
Next
Function My_PlaySample(sampletoplay, priority, vol)
Local n_channel = HaveFreeChannel()
If n_channel > 0
PlaySample(sampletoplay, {volume=vol, channel=n_channel})
samplechannelspriority[n_channel] = priority
Else
Local usechannel = -1
For Local n = 1 To TableItems(samplechannelspriority)
If priority > samplechannelspriority[n] Then usechannel = n
Next
If usechannel > -1
PlaySample(sampletoplay, {volume=vol, channel=usechannel})
samplechannelspriority[usechannel] = priority
EndIf
EndIf
EndFunction
Notice the priority variable. This ones idea is that there can be lower priority and higher priority sounds.
If there are no free channels left, then it will compare the current playing channels how high their priority is, and if priority is higher than the current playing one, will instead force to play the new sample. And vice versa as in if the sound is lower priority sound and all other channels are already occupied with higher priority stuff, then it skips playing it at all.
Also, notice that for some reason Hollywood channel numbers start from 1 instead of 0.