My findings on how to prevent a sound from overlaping itself

Started by argoon, November 05, 2017, 02:09:24 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

argoon

After many hours of trying to prevent a sound from playing over itself, when pressing a key continuously and inside the player think loop, after going trough the engine code i found some ways to solve this.

One:


const idSoundShader *sound = declManager->FindSound( "sound_name" );
// this above is not really necessary you can do the following instead if you want.
//...StartSound( declManager->FindSound( "sound_name" ), SND_CHANNEL_BODY );

// get the sound emitter (speaker) binded to the ingame entity, btw i tried to get a emitter directly from the master entity but it didn't found any and crashed the engine, why i don't know,
// is strange because afaik all entities can be "speakers"...
idSoundEmitter *emit = gameLocal.FindEntity( "some_speaker" )->GetSoundEmitter( );
if (!( emit->CurrentlyPlaying( ) )) // if sound is not playing
{
        // play it
emit->StartSound( sound, SND_CHANNEL_BODY);
}


Two:


const idSoundShader *sound = declManager->FindSound( "sound_name" );
idSoundEmitter *emit = gameLocal.FindEntity( "some_speaker" )->GetSoundEmitter( );
emit->StartSound( sound, SND_CHANNEL_BODY, 0, SSF_PLAY_ONCE ); // SSF_PLAY_ONCE prevents the sound from playing again if it is already playing



Three:  If you don't create a emiter object like above and call the sound directly from a entity it also works. :)


const idSoundShader *sound = declManager->FindSound( "sound_name" );
Entity->StartSoundShader( sound, SND_CHANNEL_BODY, SSF_PLAY_ONCE, false, NULL );