Sat May 27 2023
SourceMod CS:GO: Getting the Properties of the Weapon a Player Is Holding
With this, we can get the weapon in the player's hands. In the file, you can find:
- m_hActiveWeapon (Offset 2388) (Save)(4 Bytes)
This is the property name we're looking for.
At first I used GetEntProp to read this property directly, but it returned some weird address. Then I tried GetEntPropEnt, and it successfully read the weapon. The code is as follows:
int entity = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", 0);
I looked up the reason: GetEntProp reads an int value, while GetEntPropEnt reads an Entity Index. In CS:GO, weapons are entities.
Once you have the weapon's entity index, you have the weapon entity itself. The next step is to read this entity's properties.
int defIndex = GetEntProp(entity, Prop_Send, "m_iItemDefinitionIndex");
This code reads the weapon entity's m_iItemDefinitionIndex property. I looked it up, and this property is in Table: m_Item. It has some other properties too, such as m_iEntityQuality and m_szCustomName, and all of these can be read.
With defIndex, you can tell what weapon it is. Below is the code to read it (copied from the source of a CS:GO skin plugin):
char g_WeaponClasses[][] = {
}
Then call the ClassByDefIndex method inside it to get the class.
char weaponClass[32];
