ham_get_user_weaponent [CSTRIKE]
Posted: Wed Aug 19, 2026 9:22 am
This function returns the weapon entity index by player id weapon type, and optional weapon slot.
You can use this shortest but less efficient code :
Or the often used only fakemeta way :
Code: Select all
#define SHOTGUNS_BITSUM ((1<<CSW_XM1014)|(1<<CSW_M3))
#define SMGS_BITSUM ((1<<CSW_MAC10)|(1<<CSW_UMP45)|(1<<CSW_MP5NAVY)|(1<<CSW_TMP)|(1<<CSW_P90))
#define RIFFLES_BITSUM ((1<<CSW_AUG)|(1<<CSW_GALIL)|(1<<CSW_FAMAS)|(1<<CSW_M249)|(1<<CSW_M4A1)|(1<<CSW_SG552)|(1<<CSW_AK47))
#define SNIPERS_BITSUM ((1<<CSW_SCOUT)|(1<<CSW_SG550)|(1<<CSW_AWP)|(1<<CSW_G3SG1))
#define PRIMARIES_BITSUM (SHOTGUNS_BITSUM|SMGS_BITSUM|RIFFLES_BITSUM|SNIPERS_BITSUM)
#define GUNS_BITSUM ((1<<CSW_P228)|(1<<CSW_ELITE)|(1<<CSW_FIVESEVEN)|(1<<CSW_USP)|(1<<CSW_GLOCK18)|(1<<CSW_DEAGLE))
#define NADES_BITSUM ((1<<CSW_HEGRENADE)|(1<<CSW_SMOKEGRENADE)|(1<<CSW_FLASHBANG))
#define MAX_ITEM_TYPES 6
// These offsets handle last weapon entity that the player gained
// If the player has more than one weapon in the same slot,
// other weapons are linked to the last one with offset cbased m_pNext
// (weapon A is linked to B, weapon B is linked to C, etc..)
new const m_rgpPlayerItems_CBasePlayer[MAX_ITEM_TYPES] = {367,368,...} // added CBasePlayer to offset name because CWeaponBoc class has same offset name
// "weapon_..." offsets
#define LD_CBasePlayerItem 4
// This offset is used to link a weapon with another one
#define m_pNext 42
// This offset handles CSW_... values of the weapons
#define m_iId 43
ham_get_user_weaponent(id, iCswId, iSlot = 0)
{
new iWeapon
if( !iSlot )
{
if( PRIMARIES_BITSUM & (1<<iCswId) ) iSlot = 1
else if( GUNS_BITSUM & (1<<iCswId) ) iSlot = 2
else if( iCswId == CSW_KNIFE ) iSlot = 3
else if( NADES_BITSUM & (1<<iCswId) ) iSlot = 4
else if( iCswId == CSW_C4 ) iSlot = 5
}
// get the last weapon that the player gained in this slot
iWeapon = get_pdata_cbase(id, m_rgpPlayerItems_CBasePlayer[iSlot])
while( pev_valid(iWeapon) ) // >0 check should be enough, i let pev_valid for the tut
{
// Check if the weapon type (CSW_) is the one we are searching
if( get_pdata_int(iWeapon, m_iId, LD_CBasePlayerItem) == iCswId ) // can use cs_get_weapon_id(iWeapon) instead
{
// return the weapon entity index
return iWeapon
}
// if not, search if another weapon is linked to the previous one
iWeapon = get_pdata_cbase(iWeapon, m_pNext, LD_CBasePlayerItem)
}
// return 0 on failure
return 0
}Code: Select all
#define MAX_ITEM_TYPES 6
new const m_rgpPlayerItems_CBasePlayer[MAX_ITEM_TYPES] = {367,368,...}
#define LD_CBasePlayerItem 4
#define m_pNext 42
#define m_iId 43
ham_get_user_weaponent2(id, iCswId)
{
new iWeapon
for(new i=1; i<MAX_ITEM_TYPES; i++)
{
iWeapon = get_pdata_cbase(id, m_rgpPlayerItems_CBasePlayer[i])
while( pev_valid(iWeapon) )
{
if( get_pdata_int(iWeapon, m_iId, LD_CBasePlayerItem) == iCswId )
{
return iWeapon
}
iWeapon = get_pdata_cbase(iWeapon, m_pNext, LD_CBasePlayerItem)
}
}
return 0
} Code: Select all
fm_get_user_weaponent(id, iCswId)
{
new szWeaponName[20]
if( !get_weaponname(iCswId, szWeaponName, charsmax(szWeaponName)) )
return FM_NULLENT
new iWeapon = FM_NULLENT
while( (iWeapon = engfunc( EngFunc_FindEntityByString, iWeapon, "classname", szWeaponName )) > 0
&& pev(iWeapon, pev_owner) != id )
{ /* do nothing */ }
return iWeapon
}