Give weapon after respawn

Please post any questions about developing your plugin here. Please use the search function before posting!
ReQ220
Junior Member
Posts: 9
Joined: Sat Mar 28, 2020 7:25 pm

Give weapon after respawn

Postby ReQ220 » Thu May 21, 2020 2:17 am

Hello everyone! I'm trying to add a new command for this mod
All I want is when a player dies, he would respawn in the same place he died and with the same weapon.
Everything is working fine except, he respawns in the same location he died, however, the weapon doesn't work.
Can someone help me with this code, thank you in advance!!!

Code: Select all

@TypedServerCommand('wcs_spawn_death')
def wcs_spawn_death_command(command_info, player:convert_userid_to_player, force:int=0):
    if player is None:
        return
    weapon = player.last_weapon
    location = player.origin
    player.spawn(force)
    player.teleport(location)
    player.delay(10, player.give_named_item, (weapon,))

This command triggers after player's death (player_death)
The error that I get is:

Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/packages/source-python/listeners/tick.py", line 80, in _tick
    self.pop(0).execute()
  File "../addons/source-python/packages/source-python/listeners/tick.py", line 161, in execute
    return self.callback(*self.args, **self.kwargs)
  File "../addons/source-python/packages/source-python/entities/_base.py", line 844, in _callback
    callback(*args, **kwargs)
  File "../addons/source-python/packages/source-python/entities/helpers.py", line 96, in __call__
    *self.wrapper(self.wrapped_self, *args, **kwargs))
  File "../addons/source-python/packages/source-python/memory/helpers.py", line 331, in __call__
    return super().__call__(self._this, *args)
  File "<string>", line 1, in <lambda>

TypeError: No registered converter was able to extract a C++ pointer to type char from this Python object of type int


If I do print(weapon) I get "-1"
Thank you guys in advance!!!
User avatar
Kami
Global Moderator
Posts: 154
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Re: Give weapon after respawn

Postby Kami » Thu May 21, 2020 10:37 am

Hey,

player_death seems to be too late to fetch the weapon data. You can use player_hurt and check if the remaining health of the player is less or equal to 0.

Syntax: Select all

@Event('player_hurt')
def player_hurt(ev):
if int(ev['health']) <= 0:
#dostuff


For the command itself you could try something like this:

Syntax: Select all

@TypedServerCommand('wcs_spawn_death')
def wcs_spawn_death_command(command_info, player:convert_userid_to_player, force:int=0):
if player is None:
return
location = player.origin
player.spawn(force)
player.teleport(location)
weapons = player.weapons()
for weapon in weapons:
if weapon.weapon_name != "weapon_knife":
player.delay(10, player.give_named_item, (weapon.weapon_name,))


This would get all the weapons the player had when he died and give them to the player. I could not test it as I don't have a WCS server set up, but the idea should be clear.

If you just want primary weapons to be given to the player you could use is_filters.

Syntax: Select all

weapons = player.weapons(is_filters='primary')

Return to “Plugin Development Support”

Who is online

Users browsing this forum: Bing [Bot] and 12 guests