Page 1 of 1

Delete Player Weapons On Death.

Posted: Sat Apr 17, 2021 1:45 pm
by Grubbsy
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

Re: Delete Player Weapons On Death.

Posted: Sat Apr 17, 2021 3:30 pm
by L'In20Cible
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.

Re: Delete Player Weapons On Death.

Posted: Mon Apr 19, 2021 7:31 pm
by Predz
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()

Re: Delete Player Weapons On Death.

Posted: Wed Apr 21, 2021 12:54 am
by Grubbsy
The code works well, except for when you headshot a player with armor lol.

Re: Delete Player Weapons On Death.

Posted: Wed Apr 21, 2021 2:06 am
by satoon101
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.

Re: Delete Player Weapons On Death.

Posted: Wed Apr 21, 2021 2:17 am
by VinciT
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.

Re: Delete Player Weapons On Death.

Posted: Thu Apr 22, 2021 8:05 am
by Jezza
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()

Re: Delete Player Weapons On Death.

Posted: Sat Apr 24, 2021 2:31 pm
by Grubbsy
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.