Page 1 of 1

[STOCK] give_user_weapon

Posted: Sun Oct 04, 2026 2:31 pm
by Archive-Poster
Give User Weapon

This is pro-variant of default native give_item()

What's the difference between mine and default one?
Check if user is alive
Check if user already have respective weapon and set only clip/ammo without giving him the weapon and throw error.
Ability to set Clip of weapon
Ability to set BPAmmo of the weapon
Return the name of weapon without weapon_ prefix!
Return properly the Weapon Entity ID you give
Use CSW_ constants to give an weapon to a player instead of weapon_ id.
Error with "native cs_set_weapon_ammo" has been resolved.

OverView

Code: Select all

/*  
    give_user_weapon( index, CSW_weapon, Clip, BPAmmo, szWeapon, maxchars )

        @param index: The Player Index to give an weapon.
        @param iWeaponTypeID: Weapon ID ( CSW_* )
        @param iClip: Clip of ammo (bullets).
        @param iBPAmmo: Back Pack ammo of the weapon.
        @param szWeapon[]:(Optionaly) The weapon name pointer to receive the weapon name. 
                                              The Weapon name is without 'weapon_' prefix. Example: m4a1

        @param maxchars: (Optionaly if you use szWeapon param ) szWeapon[] size. The value 20 is recommended for most weapons.

                @return: Weapon Entity ID
*/

 give_user_weapon( index , iWeaponTypeID , iClip=0 , iBPAmmo=0 , szWeapon[]="" , maxchars=0)
Function

Code: Select all

give_user_weapon( index , iWeaponTypeID , iClip=0 , iBPAmmo=0 , szWeapon[]="" , maxchars=0 )
{
	if ( !( CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip < 0 ) || ( iBPAmmo < 0 ) || !is_user_alive( index ) )
		return -1;
	
	new szWeaponName[ 20 ] , iWeaponEntity , bool:bIsGrenade;
	
	const GrenadeBits = ( ( 1 << CSW_HEGRENADE ) | ( 1 << CSW_FLASHBANG ) | ( 1 << CSW_SMOKEGRENADE ) | ( 1 << CSW_C4 ) );
	
	if ( ( bIsGrenade = bool:!!( GrenadeBits & ( 1 << iWeaponTypeID ) ) ) )
		iClip = clamp( iClip ? iClip : iBPAmmo , 1 );
	
	get_weaponname( iWeaponTypeID , szWeaponName , charsmax( szWeaponName ) );
	
	if ( ( iWeaponEntity = user_has_weapon( index , iWeaponTypeID ) ? find_ent_by_owner( -1 , szWeaponName , index ) : give_item( index , szWeaponName ) ) > 0 )
	{
		if ( iWeaponTypeID != CSW_KNIFE )
		{
			if ( iClip && !bIsGrenade )
				cs_set_weapon_ammo( iWeaponEntity , iClip );
		
			if ( iWeaponTypeID == CSW_C4 ) 
				cs_set_user_plant( index , 1 , 1 );
			else
				cs_set_user_bpammo( index , iWeaponTypeID , bIsGrenade ? iClip : iBPAmmo ); 
		}
		
		if ( maxchars )
			copy( szWeapon , maxchars , szWeaponName[7] );
	}
	
	return iWeaponEntity;
}
Required modules:
CStrike, Engine, Fun.

Usage Example:

Code: Select all

// Give to a index a deagle with 1 clip and 0 back pack ammo in stock.
give_user_weapon( id, CSW_DEAGLE, 1, 0 );

Code: Select all

// Give to a index 10 hegrenades:
give_user_weapon( id, CSW_HEGRENADE, 5 );