Raised This Month: $51 Target: $400
 12% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 03-29-2016 , 23:33   [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #1

[Code Snippet] 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.

By other hand, this tutorial has been very useful: https://forums.alliedmods.net/showthread.php?t=41265


│ 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"



PHP Code:
/*================================================================================
 [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(PLUGINVERSIONAUTHOR)
    
    
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_wpEventname)) g_eventFiveSeven get_orig_retval();

public 
fw_update_clientdata(idsendweaponscd_handle)
{
    if(!
is_user_alive(id))
        return 
FMRES_IGNORED;

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

public 
fw_playback_event(flagsinvokereventidFloat:delayFloat:origin[3], Float:angles[3], Float:fparam1Float:fparam2iParam1iParam2bParam1bParam2)
{
    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_PlaybackEventflags FEV_HOSTONLYinvokereventiddelayoriginanglesfparam1fparam2iParam1iParam2bParam1bParam2)
        
        
/* 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_SendWeaponAniminvokereventid); // invoker = Player index | eventid = Entity index | 1 = animation sequence of the model (fire animation)
        
emit_sound(invokerCHAN_WEAPONg_sndCustomWeapon1.0ATTN_NORM0PITCH_NORM); // Emit the custom "fire" sound
        
        
return FMRES_SUPERCEDE;
    }
    
    return 
FMRES_IGNORED;
}

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

// Thx to Arkshine
stock UTIL_SendWeaponAnimownerweapon 0sequence )
{
    if (!
weapon)
    {
        const 
m_pActiveItem 373;
        
weapon get_pdata_cbaseownerm_pActiveItem, .linuxdiff 5, .macdiff );
    }
   
    
set_pevownerpev_weaponanimsequence );

    if ( 
ExecuteHamHam_Weapon_UseDecrementweapon ) && engfuncEngFunc_CanSkipPlayerowner ) )
        return;
    
    
message_beginMSG_ONESVC_WEAPONANIM, .player owner );
    
write_bytesequence );
    
write_bytepevweaponpev_body ) );
    
message_end( );

And that's all my friends, it has not any complexity.

│ Test
__________________

Last edited by Nax0ne; 09-18-2022 at 09:15.
Nax0ne is offline
meTaLiCroSS
Gaze Upon My Hat
Join Date: Feb 2009
Location: Viña del Mar, Chile
Old 03-30-2016 , 00:31   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #2

For sure it has no complexity, it's on every russian-made custom weapon (dias, brp), you posted it too late for giving it yourself as author.
__________________
Quote:
Originally Posted by joropito View Post
You're right Metalicross
meTaLiCroSS is offline
KORD_12.7
Senior Member
Join Date: Aug 2009
Location: Russia, Vladivostok
Old 03-30-2016 , 02:04   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #3

This "method" is too old.
__________________

Vi Veri Veniversum Vivus Vici
Russian Half-Life and Adrenaline Gamer community
KORD_12.7 is offline
Send a message via ICQ to KORD_12.7
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 03-30-2016 , 13:32   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #4

Quote:
Originally Posted by meTaLiCroSS View Post
For sure it has no complexity, it's on every russian-made custom weapon (dias, brp), you posted it too late for giving it yourself as author.
I know... but anyway, this is very useful for those people who do not know this method. Not everyone knows how to change the sound of the gun, so... this is very helpful for users who need this.

Quote:
Originally Posted by KORD_12.7 View Post
This "method" is too old.
But it is efficient for those who do not know how to change the sound of the gun.

If you recognize another method, you can share it with the community. It would be very helpful.
__________________
Nax0ne is offline
SkumTomteN
Veteran Member
Join Date: Oct 2013
Location: Asgard
Old 03-31-2016 , 19:18   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #5

you can hook with Ham_Weapon_PrimaryAttack in post.
__________________
Contact: Steam
Videos: Youtube
SkumTomteN is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 03-31-2016 , 22:52   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #6

I was searching that, lol
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
KORD_12.7
Senior Member
Join Date: Aug 2009
Location: Russia, Vladivostok
Old 04-01-2016 , 05:44   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #7

Nax0ne, your code is redundant and have typo error in UTIL_weapon_anim.
__________________

Vi Veri Veniversum Vivus Vici
Russian Half-Life and Adrenaline Gamer community
KORD_12.7 is offline
Send a message via ICQ to KORD_12.7
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 04-01-2016 , 10:33   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #8

Quote:
Originally Posted by KORD_12.7 View Post
Nax0ne, your code is redundant and have typo error in UTIL_weapon_anim.
What error in UTIL_weapon_anim? Can you explain me?

Anyway, it works correctly. I tested it.

Quote:
Originally Posted by SkumTomteN View Post
you can hook with Ham_Weapon_PrimaryAttack in post.
The problem of that forward, is that also detects if the player is holding the button "+attack" and it happens the problem that the weapon fires infinitely (and the sound is repetitive and annoying)

For that reason, I don't use it.
__________________

Last edited by Nax0ne; 04-01-2016 at 19:48.
Nax0ne is offline
NiHiLaNTh
Way Past Expiration
Join Date: May 2009
Location: Latvia
Old 04-01-2016 , 15:54   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #9

Code:
write_byte(pev(index, pev_body));

Here's your mistake.
gl finding it
__________________


Last edited by NiHiLaNTh; 04-01-2016 at 15:54.
NiHiLaNTh is offline
Send a message via Skype™ to NiHiLaNTh
Nax0ne
Senior Member
Join Date: Jul 2011
Location: Chile
Old 04-01-2016 , 16:43   Re: [Code Snippet] Method to change "fire" sound of any weapon
Reply With Quote #10

Quote:
Originally Posted by NiHiLaNTh View Post
Code:
write_byte(pev(index, pev_body));

Here's your mistake.
gl finding it
Should I use entity_get instead of pev?
__________________
Nax0ne is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:22.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode