View Single Post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-26-2016 , 19:12   Re: [STOCK] give_user_weapon
Reply With Quote #5

I made a few tweaks to it.
  • Made it return a useful value. The weapon ID is already known since it is being passed to the function, so why return it? Instead, return the entity ID since this is unknown information and the scripter may want to use it for other stuff. If the checks fail, the function returns -1.
  • Renamed wp_id variable to something that makes sense since it is holding the weapon name, not id.
  • Added additional checks to prevent errors. Weapon ID must fall within a range that will make get_weaponname() not error. Clip and bpammo must be >= 0.
  • Eliminated if-else and redundant code by getting the entity index in one line based on if the player has it already or if it is given to the player.
  • Final safety check to make sure an entity index is in the variable before using cs_set ammos.
  • Added grenade support.
  • Fixed C4 support and added code to conditionally give clip/bp ammo.
PHP Code:
give_user_weaponindex iWeaponTypeID iClip=iBPAmmo=)
{
    if ( !( 
CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip ) || ( iBPAmmo ) || !is_user_aliveindex ) )
        return -
1;
    
    new 
szWeaponName20 ] , iWeaponEntity GrenadeBits;
    
    
GrenadeBits = ( ( << CSW_HEGRENADE ) | ( << CSW_FLASHBANG ) | ( << CSW_SMOKEGRENADE ) | ( << CSW_C4 ) );
    
get_weaponnameiWeaponTypeID szWeaponName charsmaxszWeaponName ) );
    
    if ( ( 
iWeaponEntity user_has_weaponindex iWeaponTypeID ) ? find_ent_by_owner( -szWeaponName index ) : give_itemindex szWeaponName ) ) > )
    {
        if ( 
iClip && !( GrenadeBits & ( << iWeaponTypeID ) ) )
            
cs_set_weapon_ammoiWeaponEntity iClip );

        if ( 
iWeaponTypeID == CSW_C4 
            
cs_set_user_plantindex );
        else if ( 
iBPAmmo )
            
cs_set_user_bpammoindex iWeaponTypeID iBPAmmo ); 
    }
    
    return 
iWeaponEntity;

__________________

Last edited by Bugsy; 01-28-2016 at 18:50.
Bugsy is offline