HL2dm - Getting error on headshot.

All other Source.Python topics and issues.
User avatar
daren adler
Senior Member
Posts: 328
Joined: Sat May 18, 2019 7:42 pm

HL2dm - Getting error on headshot.

Postby daren adler » Fri May 29, 2020 2:48 am

Code: Select all

2020-05-28 21:12:47 - sp   -   MESSAGE   [SP] Successfully loaded plugin 'headshot'.
2020-05-28 21:23:37 - sp   -   EXCEPTION   
[SP] Caught an Exception:
Traceback (most recent call last):
  File "..\addons\source-python\plugins\headshot\headshot.py", line 93, in _pre_take_damage
    _damage_weapons[victim.userid] = Player(entity.index).active_weapon.classname
  File "..\addons\source-python\packages\source-python\players\_base.py", line 824, in get_active_weapon
    return Weapon(index)
  File "..\addons\source-python\packages\source-python\entities\_base.py", line 132, in __call__
    obj = super().__call__(index)
  File "..\addons\source-python\packages\source-python\weapons\_base.py", line 40, in __init__
    WeaponMixin.__init__(self, index)

ValueError: Index '78' is not a valid weapon.


Heres the script i am using

Syntax: Select all

from contextlib import suppress

from engines.sound import Sound
from engines.precache import Model
from entities import TakeDamageInfo
from entities.entity import BaseEntity, Entity
from entities.hooks import EntityCondition, EntityPreHook
from events import Event
from listeners.tick import Delay
from memory import make_object
from players.constants import HitGroup
from players.entity import Player
from stringtables.downloads import Downloadables


# =============================================================================
# >> CONFIGURATION
# =============================================================================
REMOVE_OVERLAY_TIME = 1.5

HEADSHOT_OVERLAY_VMT_ATTACKER = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF_ATTACKER = "exae/rocksheadshot1.vtf"

HEADSHOT_OVERLAY_VMT_VICTIM = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF_VICTIM = "exae/rocksheadshot1.vtf"

# Headshot but not dead sound + path
HEADSHOT_SOUND = 'exae/quake/human-hit-4.mp3'

# <weapon> : <headshot_sound>
WEAPON_FILES = {
'weapon_crossbow' : 'exae/quake/headshot2.mp3',
'weapon_357' : 'exae/quake/headshot-2.mp3',
'weapon_alyxgun' : 'exae/quake/headshot-2.mp3',
'weapon_smg1' : 'exae/quake/wife-headshot.mp3',
'weapon_ar2' : 'exae/quake/boomheadshot.mp3',
'weapon_pistol' : 'exae/quake/headshot-1.mp3',
'weapon_shotgun' : 'exae/quake/headshot2.mp3'
}

DEAD_MODEL_PATH = 'models/headless_body/skeleton_rave.mdl'
DEAD_MODEL_DL_FILES = [
'models/headless_body/skeleton_rave.dx80.vtx',
'models/headless_body/skeleton_rave.dx90.vtx',
'models/headless_body/skeleton_rave.mdl',
'models/headless_body/skeleton_rave.phy',
'models/headless_body/skeleton_rave.sw.vtx',
'models/headless_body/skeleton_rave.vvd'
]
# =============================================================================
# >> END OF CONFIGURATION
# =============================================================================

HEADSHOT_SOUND = Sound(HEADSHOT_SOUND, download=True)
WEAPON_FILES = {x: Sound(y, download=True) for x, y in WEAPON_FILES.items()}
downloads = Downloadables()
for item in (HEADSHOT_OVERLAY_VMT_ATTACKER, HEADSHOT_OVERLAY_VTF_ATTACKER,
HEADSHOT_OVERLAY_VMT_VICTIM, HEADSHOT_OVERLAY_VTF_VICTIM):
downloads.add('materials/' + item)

for item in DEAD_MODEL_DL_FILES:
downloads.add(item)

DEAD_MODEL = Model(
path=DEAD_MODEL_PATH,
preload=True,
)

_old_models = {}
_damage_weapons = {}


@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_take_damage(stack_data):
victim = make_object(Entity, stack_data[0])
if not victim.is_player():
return

victim = Player(victim.index)
info = make_object(TakeDamageInfo, stack_data[1])

# World damage?
if info.attacker == 0:
_damage_weapons[victim.userid] = 'worldspawn'

# Was a projectile used?
elif info.attacker != info.inflictor:
_damage_weapons[victim.userid] = Entity(info.inflictor).classname

# Not a projectile
else:
entity = BaseEntity(info.attacker)
if entity.is_player():
_damage_weapons[victim.userid] = Player(entity.index).active_weapon.classname
else:
_damage_weapons[victim.userid] = entity.classname


@Event('player_hurt')
def player_hurt(game_event):
victim = Player.from_userid(game_event['userid'])
attacker_userid = game_event['attacker']

# Suicide?
if attacker_userid in (victim.userid, 0):
return

# Not a headshot?
if victim.hitgroup != HitGroup.HEAD:
return

# Victim still alive?
if game_event['health']:
HEADSHOT_SOUND.index = victim.index
HEADSHOT_SOUND.play(victim.index)
return

weapon_sound = WEAPON_FILES.get(_damage_weapons.get(victim.userid))
if weapon_sound is None:
return

attacker = Player.from_userid(attacker_userid)

# Set the victim's overlay
victim.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT_VICTIM
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, [victim.userid])

# Set the attacker's overlay
attacker.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT_ATTACKER
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, [attacker_userid])

# Set the victim's model
_old_models[victim.userid] = victim.model
victim.model = DEAD_MODEL


@Event('player_spawn')
def _reset_model(game_event):
userid = game_event['userid']
old_model = _old_models.pop(userid, None)
if old_model is not None:
Player.from_userid(userid).model = old_model


def _remove_overlay(userid):
with suppress(Exception):
Player.from_userid(userid).client_command('r_screenoverlay 0')


Thank You for your help. Its not crashing the server,i just see it in the log folder/file.
Last edited by L'In20Cible on Fri May 29, 2020 5:03 am, edited 1 time in total.
Reason: [quote] → [python]
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: HL2dm - Getting error on headshot.

Postby L'In20Cible » Fri May 29, 2020 5:01 am

My first guess would be that you are using custom weapons that use class names that are not recognized as valid weapons. Try to run the code from #308#issuecomment-600340010 as a workaround for now.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: HL2dm - Getting error on headshot.

Postby L'In20Cible » Fri May 29, 2020 5:07 pm

Added a fallback check to handle such cases: 6c6b84
User avatar
daren adler
Senior Member
Posts: 328
Joined: Sat May 18, 2019 7:42 pm

Re: HL2dm - Getting error on headshot.

Postby daren adler » Fri May 29, 2020 8:00 pm

Thank you ;) I will get it a try. and yes,,what i did find out,from messing around with it is, I was using skins from HL:Alyx models, and the headshot did not work for them at all,no sound,no writing,no overlay. that was what the error was coming from,and from what you said makes it that also. So thank you very much and i will use what you said, but,,without using them HL:Alyx skins, it dont make errors anyway, i will add them back on and see if that will work!! THANK YOU for all your help ;) UPDATE-- Yes now it does not give errors on log, thank you. Just to let you know that HL:Alyx skin models will not give sound or anything, I know its not your guys stuff,,its becouse they were made for VR,

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 11 guests