Page 3 of 3

Re: Stripping weapons

Posted: Mon Dec 04, 2017 2:44 am
by L'In20Cible
You should probably call drop_weapon() prior remove() tho so the weapons are no longer mapped in the m_hMyWeapons array for that player. Doesn't matter for Source.Python because we ensure the handles are valid, but other plugins might not and attempt to manipulate them.

Re: Stripping weapons

Posted: Mon Dec 04, 2017 3:07 am
by satoon101
L'In20Cible wrote:You should probably call drop_weapon() prior remove() tho so the weapons are no longer mapped in the m_hMyWeapons array for that player.

Don't forget, as long as you are using Weapon instances, it will do the drop automatically:
https://github.com/Source-Python-Dev-Te ... 6f77f651bc

Re: Stripping weapons

Posted: Mon Dec 04, 2017 3:45 am
by BackRaw
BackRaw wrote:Does the player_weaponstrip entity has any benefits over calling weapon.remove()? What about iterating over the player's weapons and calling .remove() each time? Or is it just that the weapon filters are more flexible?
Ayuto wrote:No, iterating over the player's weapons and removing them is perfectly fine.

Alright cool.

Re: Stripping weapons

Posted: Tue Dec 12, 2017 1:44 pm
by battleweaver
Hello everybody!
I am puzzled a little
This code works fine

Syntax: Select all

@TypedSayCommand('!strip')
def cmd_on_strip(command_info):
player = Player(command_info.index)
weapon = Weapon((player.primary).index) # TODO: BaseEntity would be enough here?
player.drop_weapon(weapon.pointer, Vector(0, 0, 0), Vector(0, 0, 0))
weapon.remove()


While this code

Syntax: Select all

backpack = Backpack()

@OnButtonStateChanged
def on_buttons_state_changed(player, old_buttons, new_buttons):
status = get_button_combination_status(old_buttons, new_buttons,
PlayerButtons.DUCK|PlayerButtons.USE)
if status == ButtonStatus.PRESSED:
backpack.scroll(Player(player.index))

class Backpack:

def scroll(self, player):
#getting rid of weapon in hand
weapon = Entity((player.primary).index) # Tried Weapon and Entity class
print (f'Going to drop {weapon.weapon_name}')
player.drop_weapon(weapon.pointer, Vector(0, 0, 0), Vector(0, 0, 0)) #SEGMENTATION FAULT at this very line, server reboot
print ('Dropped weapon')
weapon.remove()#If I comment 2 lines above,SEGMENTATION FAULT here!
print ('Removed weapon")
#end of fragment

What am I doing wrong?

Re: Stripping weapons

Posted: Tue Dec 12, 2017 2:16 pm
by iPlayer
Try delaying weapon dropping/removing. @OnButtonStateChanged listener is based on a hook on player's run_command. Removing entities while in a hook can lead to crashes. So try this:

Syntax: Select all

weapon.delay(0, weapon.remove)


Delaying by 0 seconds doesn't execute immediately. Your callback will still be executed in a regular tick-based flow. That means outside of hooks.
If you use Weapon class, it will force-drop the weapon automatically when you call .remove()

Re: Stripping weapons

Posted: Tue Dec 12, 2017 3:13 pm
by Ayuto
In addition to what iPlayer said, I would like to add that you are doing a few redundant calls. "player.primary" already returns a Weapon instance (or None if the player has no primary). So, your scroll method should look like this:

Syntax: Select all

def scroll(self, player):
primary = player.primary
if primary is None:
return

primary.delay(0, primary.remove)