Playing sounds is great, but occasionally you may want to stop a sound that is currently being played. For example there is an execution countdown in Jailbreak that can be averted mid way through, and it was easiest to have this as a single sound that can be stopped as needed.
To do that we can add a new game event which will specify the name of a sound to stop.
The New Game Event
Firstly, in resource/modevents.res add the following new event:
1 2 3 4 | "stop_audio" { "sound" "string" } |
Listening for the Event
Now we just need to listen for the game event and act on it, so in clientmodeshared.cpp find:
1 | else if ( Q_strcmp( "teamplay_broadcast_audio", eventname ) == 0 ) |
and add a new else if clause after the closing brace for that one:
1 2 3 4 5 6 7 | else if ( Q_strcmp( "stop_audio", eventname ) == 0 ) { const char *pszSoundName = event->GetString("sound"); C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer(); if(pPlayer) pPlayer->StopSound( pszSoundName ); } |
As you can see, it’s pretty straight forward. First we get the name of the sound to stop and the local player, then if the player is valid we just call StopSound on them.
Using It
That’s all we need to do to hook up the event; now to actually use it you can simply do the following:
1 2 3 4 5 6 | IGameEvent *event = gameeventmanager->CreateEvent( "stop_audio" ); if ( event ) { event->SetString( "sound", "Jailbreak.Countdown"); gameeventmanager->FireEvent( event ); } |
Replacing ‘Jailbreak.Countdown’ with the name of the sound you wish to stop.