Delete Player Weapons On Death.

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Grubbsy
Junior Member
Posts: 12
Joined: Sat Aug 29, 2020 10:12 pm

Delete Player Weapons On Death.

Postby Grubbsy » Sat Apr 17, 2021 1:45 pm

I'm trying to have my plugin remove player weapons when they die. but only if they are Counter-Terrorists.-

Syntax: Select all

@PreEvent('player_death')
def pre_player_death(data):

victim = _players.from_userid(data['userid'])
if victim.team == 3:
print("victim was a ct")
print(list(victim.weapons()))
for weapon in list(victim.weapons()):
print("removing weapons")
weapon.remove()


This isn't working as expected, as the death prehook still doesn't start when the player has weapons.

I'm unsure if there's any other simple way other than just logging what weapons a player has when they die so I can remove those ents.


(for the sake of the plugin I'm making, I need cts to not drop weapons on dying or else it'll be given to the Ts, however, the Ts still need to be able to drop weapons for CTs to take)

Thanks! - Grubbsy
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Delete Player Weapons On Death.

Postby L'In20Cible » Sat Apr 17, 2021 3:30 pm

You could predict whether the player will die or not by comparing his health against the amount of damage taken into player_hurt and strip him of his weapons there.
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: Delete Player Weapons On Death.

Postby Predz » Mon Apr 19, 2021 7:31 pm

Something along the lines of this would probably achieve it :)

Ripped straight from my plugin so apologies that it's not optimized!

Syntax: Select all

@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_damage_call_events(stack_data):
take_damage_info = make_object(TakeDamageInfo, stack_data[1])
if take_damage_info.attacker:
entity = Entity(take_damage_info.attacker)
attacker = Player(entity.index) if entity.is_player() else None

if attacker:
victim = Player(index_from_pointer(stack_data[0]))
if victim.health <= take_damage_info.damage:
for weapon in victim.weapons():
weapon.remove()
User avatar
Grubbsy
Junior Member
Posts: 12
Joined: Sat Aug 29, 2020 10:12 pm

Re: Delete Player Weapons On Death.

Postby Grubbsy » Wed Apr 21, 2021 12:54 am

The code works well, except for when you headshot a player with armor lol.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Delete Player Weapons On Death.

Postby satoon101 » Wed Apr 21, 2021 2:06 am

Yeah, it might work better to use the player_hurt event, as L'In20Cible stated. If I remember correctly, there is an event variable for the amount of remaining health, so you can just check that to see if it's 0.
Image
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: Delete Player Weapons On Death.

Postby VinciT » Wed Apr 21, 2021 2:17 am

Grubbsy wrote:The code works well, except for when you headshot a player with armor lol.
If you hook on_take_damage_alive instead of on_take_damage, you'll get correct damage numbers which account for armor.
ImageImageImageImageImage
Jezza
Junior Member
Posts: 16
Joined: Tue Aug 28, 2012 5:52 pm

Re: Delete Player Weapons On Death.

Postby Jezza » Thu Apr 22, 2021 8:05 am

Since players drop their weapons when they die, hooking "drop_weapon" is a simplest way to implement this.

Syntax: Select all

#   Entities
from entities.hooks import EntityCondition
from entities.hooks import EntityPostHook
# Memory
from memory import make_object
# Players
from players.entity import Player
# Weapons
from weapons.entity import Weapon

@EntityPostHook(EntityCondition.is_player, "drop_weapon")
def post_drop_weapon(args, return_value):
try:
player = make_object(Player, args[0])
except ValueError:
return

if player.health <= 0 and player.team == 3:
weapon = make_object(Weapon, args[1])
weapon.remove()
User avatar
Grubbsy
Junior Member
Posts: 12
Joined: Sat Aug 29, 2020 10:12 pm

Re: Delete Player Weapons On Death.

Postby Grubbsy » Sat Apr 24, 2021 2:31 pm

Jezza wrote:Since players drop their weapons when they die, hooking "drop_weapon" is a simplest way to implement this.

Syntax: Select all

#   Entities
from entities.hooks import EntityCondition
from entities.hooks import EntityPostHook
# Memory
from memory import make_object
# Players
from players.entity import Player
# Weapons
from weapons.entity import Weapon

@EntityPostHook(EntityCondition.is_player, "drop_weapon")
def post_drop_weapon(args, return_value):
try:
player = make_object(Player, args[0])
except ValueError:
return

if player.health <= 0 and player.team == 3:
weapon = make_object(Weapon, args[1])
weapon.remove()

Thanks, this works perfectly! I knew I'd need a specific hook but didn't know this one existed.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 23 guests