Stripping weapons

Post Python examples to help other users.
User avatar
L'In20Cible
Project Leader
Posts: 1106
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Stripping weapons

Postby L'In20Cible » Mon Dec 04, 2017 2:44 am

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.
User avatar
satoon101
Project Leader
Posts: 2582
Joined: Sat Jul 07, 2012 1:59 am

Re: Stripping weapons

Postby satoon101 » Mon Dec 04, 2017 3:07 am

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
Image
User avatar
BackRaw
Senior Member
Posts: 507
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: Stripping weapons

Postby BackRaw » Mon Dec 04, 2017 3:45 am

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.
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: Stripping weapons

Postby battleweaver » Tue Dec 12, 2017 1:44 pm

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?
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Stripping weapons

Postby iPlayer » Tue Dec 12, 2017 2:16 pm

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()
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Ayuto
Project Leader
Posts: 1911
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Stripping weapons

Postby Ayuto » Tue Dec 12, 2017 3:13 pm

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)

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest