This tutorial shows you how to block sounds in the game that players hear. Note that it's not client-side - it's when you're standing next to another player and you'd normally hear a sound, ie. him using a radio command, turning on his flashlight, or picking up a weapon. That's the kind of sound it blocks, also known as an "emitted sound"
This tutorial uses
fakemeta, an alternative to the engine module.
-----------------------------------------
Firstly, we have to establish our global variable of the string of the sound we'd like to block. You can turn this into a multi-dimensional array if you'd like to block several sounds (but you will obviously have to loop through it - more info at the bottom of the tutorial)
Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta> // Must include this for the "hook" function
// Global Variables
new g_soundlist[] = "items/flashlight1.wav"
Now we're going to initialise our plugin. This will provide the fakemeta "hook" that catches when the game tries to "emit a sound" so we can do what we want to it before it happens on the client's end.
Code:
public plugin_init() {
register_forward(FM_EmitSound, "block_sound")
}
Now the actual sound blocking. We're basically catching every single sound that the game emits and testing if it's the sound we want to block. If it is, stop sending that sound emission to the client. Otherwise, continue what you were doing - your papers are in order citizen.
Code:
public block_sound(entity, channel, const sound[]) {
// Use equali to check strings against each other because
// they are technically arrays
if ( equali(sound, g_soundlist) )
return FMRES_SUPERCEDE
else
return FMRES_IGNORED
}
So the overall code looks like this
Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
// Global Variables
new g_soundlist[] = "items/flashlight1.wav"
public plugin_init() {
register_forward(FM_EmitSound, "block_sound")
}
public block_sound(entity, channel, const sound[]) {
if ( equali(sound, g_soundlist) )
return FMRES_SUPERCEDE
else
return FMRES_IGNORED
}
Looks pretty simple right? Let's say you wanted to block multiple sounds this time. Take a look at how a multi-dimensional array is created in the global variable and how we loop through the array in the block_sound function to test every line of the array against the sound being emitted.
Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
// Global Variables
new g_soundlist[2][64] =
{
"items/weaponpickup.wav",
"items/flashlight1.wav"
}
public plugin_init() {
register_forward(FM_EmitSound, "block_sound")
}
public block_sound(entity, channel, const sound[]) {
// Get the size of the array so you know how many
// times to loop through it
new g_listsize = sizeof g_soundlist
for(new i; i<g_listsize; i++)
{
// Checks each line of the array against the sound emitted
if( equali(sound, g_soundlist[i]) )
return FMRES_SUPERCEDE
}
// In this case, "else" isn't needed because if it were anything
// other than "else," it would have already been handled in the
// loop by exiting the function with FMRES_SUPERCEDE
return FMRES_IGNORED
}
Notice the use of FMRES_SUPERCEDE and FMRES_IGNORED.
FMRES_SUPERCEDE basically says stop doing what you were doing to the client. If you wanted to "replace" the blocked sound, you'd do it right before the FMRES_SUPERCEDE line.
FMRES_IGNORED is saying to ignore the hook and keep going with what you were doing, Mr. Emitted-Sound.
__________________