Solved Multiple SoundFX in the same animation

Question that is answered or resolved.

Okamijoe

Member
Hello,

I'm a bit confused after reading the manual, especially when it comes to using multiple .wav files in the same animation.

sound {path}
  • {path} points to a sound effect. The sound will be played as soon as the next frame is reached.
  • You can declare more than one, in different frames that is. Beware though, the previous played sound will be immediately stopped when new one is played.
I gave it a try, but it doesn't work like in the description.
Previously played sounds never stop, they keep on playing to the end, no matter how many of them are declared in the same animation.
I tested this with a bunch of different animations and sound files (Build 6391).

Has there been a change to the engine and the manual was not updated?
Is there a way to stop a sound effect from playing when reaching a certain frame without using script?

Thank you for your help!
 
So, if I understand this correctly, I'll have to combine with the script from before:
@script
if(frame == 4)
{
//PLAY SAMPLE AND SAVE THE USED CHANNEL IMMEDIATELY
playsample(loadsample("data/sounds/charge.wav"), 0, 120, 120, 100, 1);
setentityvar(getlocalvar("self"), "usedChannel", querychannel(openborvariant("sample_play_id")));
}
if(frame == 13)
{
//STOP SAMPLE WHEN REACHING A CERTAIN FRAME
stopchannel(querychannel(openborvariant("sample_play_id")));
}
@end_script
  • The first part makes the sample play at frame #4 and saves the channel
  • The second part makes the sample stop at frame #13 when it's not being interrupted by enemy attack
  • The other scripts for pain and fall animations check for the saved channel and stop it when still playing
I tested it and it actually works! That's amazing!

One question though: Did I combine that code above in a proper manner, or is the engine shaking its head in disgust? 😅
 
So, if I understand this correctly, I'll have to combine with the script from before:

  • The first part makes the sample play at frame #4 and saves the channel
  • The second part makes the sample stop at frame #13 when it's not being interrupted by enemy attack
  • The other scripts for pain and fall animations check for the saved channel and stop it when still playing
I tested it and it actually works! That's amazing!

One question though: Did I combine that code above in a proper manner, or is the engine shaking its head in disgust? 😅
Good to know that it's working! And yeah, you combined correctly but for safety I suggest at least adding one more line to clear the variable properly in the "frame 13" part too.

C:
@script
if(frame == 4)
{
//PLAY SAMPLE AND SAVE THE USED CHANNEL IMMEDIATELY
playsample(loadsample("data/sounds/charge.wav"), 0, 120, 120, 100, 1);
setentityvar(getlocalvar("self"), "usedChannel", querychannel(openborvariant("sample_play_id")));
}
if(frame == 13)
{
//STOP SAMPLE WHEN REACHING A CERTAIN FRAME
stopchannel(querychannel(openborvariant("sample_play_id")));
setentityvar(getlocalvar("self"), "usedChannel", NULL());
}
@end_script

However, the method applied on the pain/fall using a variable is more accurate because it will stop only the used channel. If you stop the sample using the openborvariant("sample_play_id") directly it will stop any played sample no matter if it's the correct sound, but you can use the method that fits better for your project.

C:
@script
if(frame == 4)
{
//PLAY SAMPLE AND SAVE THE USED CHANNEL IMMEDIATELY
playsample(loadsample("data/sounds/charge.wav"), 0, 120, 120, 100, 1);
setentityvar(getlocalvar("self"), "usedChannel", querychannel(openborvariant("sample_play_id")));
}
if(frame == 13 && getentityvar(getlocalvar("self"), "usedChannel") != NULL()) //ONLY STOP CHANNELS AT FRAME 13 IF THE VARIABLE EXISTS
{
//GET THE USED CHANNEL PREVIOUSLY SAVED, STOP THE SAMPLE AND CLEAR THE VARIABLE
stopchannel(getentityvar(getlocalvar("self"), "usedChannel"));
setentityvar(getlocalvar("self"), "usedChannel", NULL());
}
@end_script
 
Good to know that it's working! And yeah, you combined correctly but for safety I suggest at least adding one more line to clear the variable properly in the "frame 13" part too.

C:
@script
if(frame == 4)
{
//PLAY SAMPLE AND SAVE THE USED CHANNEL IMMEDIATELY
playsample(loadsample("data/sounds/charge.wav"), 0, 120, 120, 100, 1);
setentityvar(getlocalvar("self"), "usedChannel", querychannel(openborvariant("sample_play_id")));
}
if(frame == 13)
{
//STOP SAMPLE WHEN REACHING A CERTAIN FRAME
stopchannel(querychannel(openborvariant("sample_play_id")));
setentityvar(getlocalvar("self"), "usedChannel", NULL());
}
@end_script

However, the method applied on the pain/fall using a variable is more accurate because it will stop only the used channel. If you stop the sample using the openborvariant("sample_play_id") directly it will stop any played sample no matter if it's the correct sound, but you can use the method that fits better for your project.

C:
@script
if(frame == 4)
{
//PLAY SAMPLE AND SAVE THE USED CHANNEL IMMEDIATELY
playsample(loadsample("data/sounds/charge.wav"), 0, 120, 120, 100, 1);
setentityvar(getlocalvar("self"), "usedChannel", querychannel(openborvariant("sample_play_id")));
}
if(frame == 13 && getentityvar(getlocalvar("self"), "usedChannel") != NULL()) //ONLY STOP CHANNELS AT FRAME 13 IF THE VARIABLE EXISTS
{
//GET THE USED CHANNEL PREVIOUSLY SAVED, STOP THE SAMPLE AND CLEAR THE VARIABLE
stopchannel(getentityvar(getlocalvar("self"), "usedChannel"));
setentityvar(getlocalvar("self"), "usedChannel", NULL());
}
@end_script
I tested it and everything works as expected! Thank you again for your time and input! 😊
 
@Kratus
C++:
@script
if(frame == 10)
{
    int maxchannels = openborvariant("maxsoundchannels");
    int channel;

    //STOP ALL SAMPLE CHANNELS
    for(channel = 0; channel < maxchannels; channel++){
        stopchannel(channel);
    }
}
@end_script
so while ive only used this once for an enemy to stop the sound if it misses by certain frame... this is the only one that worked..the rest kept replaying the sound file automatically after 1st time the sound plays.. i have to used this on many characters will report if it doesnt work elsewhere but so far for that one character it works perfect :)
thank you for this great script :)
 
Back
Top Bottom