[Code Snippet] Method to change "fire" sound of any weapon

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

[Code Snippet] Method to change "fire" sound of any weapon

Post by Archive-Poster »

Method to change "fire" sound of any weapon

Hi, everyone!

In this code, I will you show how to replace the "fire" sound of any gun by another customizable. The method is not difficult (its old but efficient), is simple to use and solves the problem of all those who tried to change the fire sound. Many are interested in have a customizable weapon with own sounds, but quiet. This is the solution.

│ Code Snippet
First of all, you must have the "custom sound" that you want to replace instead of the original.

The weapon I used as example was the weapon "ES Five-seven"

Code: Select all

/*================================================================================
 [Main Include Files]
=================================================================================*/

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <engine>

/*================================================================================
 [Plugin Data]
=================================================================================*/

#define PLUGIN "Custom Fire Weapon: ES Five-seven"
#define VERSION "1.0"
#define AUTHOR "Nax0ne"

/*================================================================================
 [Constants]
=================================================================================*/

new const g_sndCustomWeapon[] = "weapons/my_custom_sound.wav"
new const g_wpEvent[] = "events/fiveseven.sc"

/*================================================================================
 [Global variables]
=================================================================================*/

new g_eventFiveSeven;

/*================================================================================
 [Init + Precache]
=================================================================================*/

public plugin_init() 
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    
    register_forward(FM_PlaybackEvent, "fw_playback_event");
    register_forward(FM_UpdateClientData, "fw_update_clientdata", 1);
}

public plugin_precache() 
{
    precache_sound(g_sndCustomWeapon); // Precache the custom sound
    
    register_forward(FM_PrecacheEvent, "fw_precache_event", 1); // Precache weapon event file
}

/*================================================================================
 [Fakemeta Forwards]
=================================================================================*/

public fw_precache_event(type, const name[])
    // We need to precache the event weapon (In this case: Fiveseven) to change its sound.
    /* To change sound of any weapon, you must only change the event file above, in the constant "g_wpEvent". 
       For example "events/ak47.sc" instead of "events/fiveseven.sc" */
    if(equal(g_wpEvent, name)) g_eventFiveSeven = get_orig_retval();

public fw_update_clientdata(id, sendweapons, cd_handle)
{
    if(!is_user_alive(id))
        return FMRES_IGNORED;

    static weapon;
    weapon = get_user_weapon(id)
    
    if(weapon == CSW_FIVESEVEN)
        set_cd(cd_handle, CD_flNextAttack, halflife_time() + 0.001 ); // Block the original "fire" sound event.
    
    return FMRES_IGNORED;
}

public fw_playback_event(flags, invoker, eventid, Float:delay, Float:origin[3], Float:angles[3], Float:fparam1, Float:fparam2, iParam1, iParam2, bParam1, bParam2)
{
    if(!is_user_connected(invoker))
        return FMRES_IGNORED;
    
    static weapon;
    weapon = get_user_weapon(invoker)
        
    if(weapon != CSW_FIVESEVEN)
        return FMRES_IGNORED;    
        
    if(eventid == g_eventFiveSeven)
    {
        engfunc(EngFunc_PlaybackEvent, flags | FEV_HOSTONLY, invoker, eventid, delay, origin, angles, fparam1, fparam2, iParam1, iParam2, bParam1, bParam2)
        
        /* UTIL_SendWeaponAnim(owner, weapon, sequence)
           @param index: Player index
           @param weapon: Weapon Entity index
           @param sequence: The number animation sequence (1.. 2.. 3.. 4.. etc..)
        */
        UTIL_SendWeaponAnim( invoker, eventid, 1 ); // invoker = Player index | eventid = Entity index | 1 = animation sequence of the model (fire animation)
        emit_sound(invoker, CHAN_WEAPON, g_sndCustomWeapon, 1.0, ATTN_NORM, 0, PITCH_NORM); // Emit the custom "fire" sound
        
        return FMRES_SUPERCEDE;
    }
    
    return FMRES_IGNORED;
}

/*================================================================================
 [Stocks]
=================================================================================*/

// Thx to Arkshine
stock UTIL_SendWeaponAnim( owner, weapon = 0, sequence )
{
    if (!weapon)
    {
        const m_pActiveItem = 373;
        weapon = get_pdata_cbase( owner, m_pActiveItem, .linuxdiff = 5, .macdiff = 5 );
    }
   
    set_pev( owner, pev_weaponanim, sequence );

    if ( ExecuteHam( Ham_Weapon_UseDecrement, weapon ) && engfunc( EngFunc_CanSkipPlayer, owner ) )
        return;
    
    message_begin( MSG_ONE, SVC_WEAPONANIM, .player = owner );
    write_byte( sequence );
    write_byte( pev( weapon, pev_body ) );
    message_end( );
} 
And that's all my friends, it has not any complexity.

Who is online

Users browsing this forum: No registered users and 1 guest