Page 1 of 1

AttributeError: 'NoneType' object has no attribute 'is_player'

Posted: Tue Feb 15, 2022 6:55 am
by Painkiller
Helloe People,
i found this error in the logs.

Code: Select all

2022-02-14 23:25:50 - sp   -   EXCEPTION   
[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/packages/custom/hl2mp_event_npc_death/__init__.py", line 86, in _on_entity_output
    if activator.is_player():

AttributeError: 'NoneType' object has no attribute 'is_player'


Syntax: Select all

from entities import TakeDamageInfo
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook, EntityPostHook
from events.custom import CustomEvent
from events.variable import ShortVariable, StringVariable
from events.resource import ResourceFile
from listeners import OnEntityOutput, OnNetworkedEntityDeleted
from memory import make_object
from memory.hooks import use_pre_registers
from players.helpers import userid_from_index


# Used to match entity classnames to weapons used by player_death
WEAPON_ENTITY_TO_EVENT_WEAPON = {
'npc_grenade_frag': 'grenade_frag',
'prop_physics_multiplayer': 'physics',
'prop_physics': 'physics',
'func_physbox_multiplayer': 'physics',
'func_physbox': 'physics',
'npc_tripmine': 'slam', # Planted on the surface
'npc_satchel': 'slam', # Dropped on the floor
'crossbow_bolt': 'crossbow_bolt',
'grenade_ar2': 'smg1_grenade',
'prop_combine_ball': 'combine_ball',
'rpg_missile': 'rpg_missile',
}


_take_damage_contexts = {}


def _add_take_damage_context(index, info):
_take_damage_contexts.setdefault(index, []).append(info)


def _remove_take_damage_context(index):
try:
_take_damage_contexts[index].pop()
except KeyError:
pass


def get_take_damage_context(index):
try:
return _take_damage_contexts[index][-1]
except (KeyError, IndexError):
return None


@OnNetworkedEntityDeleted
def _on_entity_deleted(entity):
_take_damage_contexts.pop(entity.index, None)


@EntityPreHook(EntityCondition.equals_entity_classname('npc_zombie'), 'on_take_damage')
def _pre_on_take_damage(args):
entity = make_object(Entity, args[0])
if not entity.classname.startswith('npc_'):
return

info = make_object(TakeDamageInfo, args[1])
_add_take_damage_context(entity.index, info)


@EntityPostHook(EntityCondition.equals_entity_classname('npc_zombie'), 'on_take_damage')
def _post_on_take_damage(args, ret_val):
with use_pre_registers(args, True):
entity = make_object(Entity, args[0])
if not entity.classname.startswith('npc_'):
return

_remove_take_damage_context(entity.index)


@OnEntityOutput
def _on_entity_output(output_name, activator, caller, value, delay):
if output_name != 'OnDeath' or not caller.classname.startswith('npc_'):
return

info = get_take_damage_context(caller.index)

with NPC_Death() as event:
event.npc_index = caller.index
event.npc_classname = caller.classname

if activator.is_player():
event.attacker = userid_from_index(activator.index)
else:
event.attacker = 0

event.weapon = ''
event.weapon_classname = ''

if info is not None:
try:
weapon = Entity(info.weapon)
except (ValueError, OverflowError):
try:
weapon = Entity(info.inflictor)
except (ValueError, OverflowError):
weapon = None

if weapon is not None:
if weapon.classname.startswith('weapon_'):
event.weapon = weapon.classname[7:]
else:
event.weapon = WEAPON_ENTITY_TO_EVENT_WEAPON.get(weapon.classname, '')

event.weapon_classname = weapon.classname


class NPC_Death(CustomEvent):
npc_index = ShortVariable("index of the NPC")
attacker = ShortVariable("userid of the player who killed the NPC")
npc_classname = StringVariable("classname of the NPC")
weapon = StringVariable("weapon used to kill the NPC (as in player_death)")
weapon_classname = StringVariable("classname of the weapon used to kill the NPC")


res_file = ResourceFile('hl2mp_event_npc_death', NPC_Death)
res_file.write()
res_file.load_events()