weapon_fire event on leftclick

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 1:02 am

is there a way to see if a player has used rightclicked knifeattack? weapon_fire e.g. only registers the leftclick attack.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: weapon_fire event on leftclick

Postby L'In20Cible » Tue Jan 24, 2017 1:35 am

The function you want to hook is CBaseCombatWeapon::SecondaryAttack.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: weapon_fire event on leftclick

Postby satoon101 » Tue Jan 24, 2017 2:27 am

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.
Image
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 5:04 am

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?
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 5:08 am

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..
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: weapon_fire event on leftclick

Postby satoon101 » Tue Jan 24, 2017 5:14 am

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):
...
Image
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 5:22 am

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..
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 5:34 am

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?
Last edited by L'In20Cible on Tue Jan 24, 2017 5:38 am, edited 1 time in total.
Reason: [code] → [python]
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: weapon_fire event on leftclick

Postby L'In20Cible » Tue Jan 24, 2017 5:40 am

Syntax: Select all

owner = entity.owner
if owner is not None and owner.is_player():
player = Player(owner.index)
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 5:41 am

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)
Last edited by L'In20Cible on Tue Jan 24, 2017 5:45 am, edited 1 time in total.
Reason: [code] → [python]
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: weapon_fire event on leftclick

Postby D3CEPTION » Tue Jan 24, 2017 5:43 am

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?
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: weapon_fire event on leftclick

Postby L'In20Cible » Tue Jan 24, 2017 5:44 am

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].
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: weapon_fire event on leftclick

Postby L'In20Cible » Tue Jan 24, 2017 5:46 am

As a side note, dir() will prints out more properties due to the following reason: viewtopic.php?f=20&t=1433&p=9516#p9516

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 18 guests