Page 1 of 1

weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 1:02 am
by D3CEPTION
is there a way to see if a player has used rightclicked knifeattack? weapon_fire e.g. only registers the leftclick attack.

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 1:35 am
by L'In20Cible
The function you want to hook is CBaseCombatWeapon::SecondaryAttack.

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 2:27 am
by satoon101
CKnife::PrimaryAttack and CKnife::SecondaryAttack are already included in our entity data. However, I am going to change it so that we include CBaseCombatWeapon::PrimaryAttack and CBaseCombatWeapon::SecondaryAttack instead. Either way, this should work fine for you:

Syntax: Select all

from entities.hooks import EntityCondition, EntityPreHook

is_knife = EntityCondition.equals_entity_classname('weapon_knife')

@EntityPreHook(is_knife, 'secondary_attack')
def _secondary_knife_attack(stack_data):
...


Also, I moved this to a more appropriate forum.

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:04 am
by D3CEPTION
mhh thanks so far. my goal really is to do this for all players, and all weapons they carrry. basically a replica of weapon_fire event, only for primary AND secondary. so if i did this with your method, are there any disadvantages in creating multiple prehooks?

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:08 am
by D3CEPTION
actually i think i will just use the weapon_fire event that i mentioned and then use your method for those weapons that are not getting fired in the event. i dont think there will be many exceptions..

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:14 am
by satoon101
I'm not sure why you would want to do this for all weapons, to be honest. I tested with a couple that don't even have a secondary fire capability, and it spams the function. I could see the merit in doing it for the weapons that actually do have a secondary fire capability, though. And no, they would each need their own specific hook. CKnife::SecondaryAttack is what gets hooked in my example above. The same will hold for CWeaponM4A1::SecondaryAttack, CWeaponGlock::SecondaryAttack, etc..., though I have not tested each one.

If the code in the hook is exactly the same, though, you can always use multiple hooks on the same function:

Syntax: Select all

from entities.hooks import EntityCondition, EntityPreHook

is_knife = EntityCondition.equals_entity_classname('weapon_knife')
is_m4a1 = EntityCondition.equals_entity_classname('weapon_m4a1')
is_glock = EntityCondition.equals_entity_classname('weapon_glock')

@EntityPreHook(is_knife, 'secondary_attack')
@EntityPreHook(is_m4a1, 'secondary_attack')
@EntityPreHook(is_glock, 'secondary_attack')
def _secondary_attack(stack_data):
...

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:22 am
by D3CEPTION
well i tried to use this: EntityCondition.equals_datamap_classname('CWeaponCSBase') but im also getting no secondary attack. does the stackdata contain the players index? cant really find any info on it anywhere..

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:34 am
by D3CEPTION

Syntax: Select all

is_knife = EntityCondition.equals_entity_classname('weapon_knife')
@EntityPreHook(is_knife, 'secondary_attack')
def _secondary_knife_attack(stack_data):
entity = Weapon(index_from_pointer(stack_data[0]))
print(entity.weapon_name)


i have this, but is there a way to get the owner index of the weapon?

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:40 am
by L'In20Cible

Syntax: Select all

owner = entity.owner
if owner is not None and owner.is_player():
player = Player(owner.index)

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:41 am
by D3CEPTION
well i cant really update my thread so ill repost a solution

Syntax: Select all

@EntityPreHook(is_knife, 'secondary_attack')
def _secondary_knife_attack(stack_data):
weapon = Weapon(index_from_pointer(stack_data[0]))
print(weapon.owner.index,weapon.weapon_name)

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:43 am
by D3CEPTION
oh yeah, i just found that too. but how come http://wiki.sourcepython.com/developing ... ity.Weapon doesnt show all directory methods of an object, while dir() in python does?

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:44 am
by L'In20Cible
D3CEPTION wrote:oh yeah, i just found that too. but how come http://wiki.sourcepython.com/developing ... ity.Weapon doesnt show all directory methods of an object, while dir() in python does?

It does. It tells you Weapon inherits from Entity, and Entity document that property: http://wiki.sourcepython.com/developing ... tity.owner

Also, Please, use [python] instead of [code].

Re: weapon_fire event on leftclick

Posted: Tue Jan 24, 2017 5:46 am
by L'In20Cible
As a side note, dir() will prints out more properties due to the following reason: viewtopic.php?f=20&t=1433&p=9516#p9516