weapon giver on activate? [bms]

A place for requesting new Source.Python plugins to be made for your server.

Please request only one plugin per thread.
User avatar
hugo5511
Junior Member
Posts: 25
Joined: Mon Jun 29, 2015 1:20 am

weapon giver on activate? [bms]

Postby hugo5511 » Sun Jul 05, 2015 2:44 am

I have tried this but no luck:

Code: Select all

from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid

@Event
def player_say(GameEvent):
    # Get the player's userid
    userid = GameEvent.GetInt('userid')

    # Get the player's index
    index = index_from_userid(userid)

    # Get the player's PlayerEntity instance
    player = PlayerEntity(index)

    # Give the player the weapon
    player.give_named_item('item_longjump', 0, True)


I'am trying to give the players weapons on activate rather than have them run around and look for them

weapons/items to spawn with after every death.

item_longjump
item_weapon_shotgun
item_weapon_mp5
item_weapon_357
item_weapon_crossbow

Thanks
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Sun Jul 05, 2015 11:02 am

You should have received errors when testing your code. it's <gameevent>.get_int(), not GetInt().
Also in CSGO I have to use 4 parameters in give_named_item(), I'm pretty sure it's the same in BMS.


Syntax: Select all

from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid


weapons = [
'item_longjump',
'item_weapon_shotgun',
'item_weapon_mp5',
'item_weapon_357',
'item_weapon_crossbow'
]

@Event
def player_spawn(game_event):
# Get the player's userid
userid = game_event.get_int('userid')

# Get the player's index
index = index_from_userid(userid)

# Get the player's PlayerEntity instance
player = PlayerEntity(index)

# Give the player the weapon
for weapon in weapons:
player.give_named_item(weapon, 0, None, False)
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Jul 05, 2015 11:48 am

Well, that's not going to work right now, anyway. We haven't added give_named_item to BMS' data, yet. I will try to figure this one out later today. Also, those weapon names don't seem correct to me. We do have weapon data for BMS, so it would probably be better to loop through those weapons and find if the player owns the weapon or not before giving them a new one.
Image
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Sun Jul 05, 2015 12:23 pm

I don't think either that weapons have an item_ prefix, I just copy pasted it. For now you can use this:


Syntax: Select all

from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid

from entities.entity import Entity
from entities.helpers import create_entity, spawn_entity


weapons = [
'item_longjump',
'item_weapon_shotgun',
'item_weapon_mp5',
'item_weapon_357',
'item_weapon_crossbow'
]

@Event
def player_spawn(game_event):
# Get the player's userid
userid = game_event.get_int('userid')

# Get the player's index
index = index_from_userid(userid)

# Get the player's PlayerEntity instance
player = PlayerEntity(index)

# Make sure the player isn't dead (player_spawn also fires when player_activate gets fired)
if player.isdead:
return

# Give the player the weapon
for weapon in weapons:

# Check if the player has this weapon

if player.get_weapon_index(weapon):

# Don't do anything
continue

# Create a new weapon entity and spawn it at the player's origin
entity = Entity(create_entity(weapon))
entity.origin = player.origin
spawn_entity(entity.origin)
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Jul 05, 2015 12:36 pm

I am working on adding give_named_item, and I noticed something. There are actually 3 integer arguments (after the string argument). However, the SDK itself only shows the one for iSubType, just like CS:S.

Also, a couple shortcuts you might not be aware of, stonedegg:

Syntax: Select all

entity = Entity.create(weapon)
entity.origin = player.origin
entity.spawn()
Image
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Sun Jul 05, 2015 1:56 pm

satoon101 wrote:Also, a couple shortcuts you might not be aware of, stonedegg:

Syntax: Select all

entity = Entity.create(weapon)
entity.origin = player.origin
entity.spawn()



Good to know, thanks.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Jul 05, 2015 7:58 pm

I added give_named_item for BMS to the repository. Having done some testing, I figured out that the 2nd of the 3 integer values is the amount of ammo to add to the player's reserve ammo. The 1st is the iSubType, like other games. I haven't figured out the 3rd, yet, as it always seems to be -1 in my function hook. I also noticed in my hook that the regular names are used (weapon_357, weapon_mp5, etc...) and not the ones with the item_ prefix. Using dumpentityfactories does show that there are entity names for both with and without the item_ prefix:

Code: Select all

...
item_battery
item_crate
item_grenade_mp5
item_grenade_rpg
item_healthcharger
item_healthkit
item_healthkit_ragdoll_clone
item_healthvial
item_longjump
item_sodacan
item_suit
item_suitcharger
item_syringe
item_tow_missile
item_weapon_357
item_weapon_assassin_glock
item_weapon_crossbow
item_weapon_crowbar
item_weapon_frag
item_weapon_glock
item_weapon_gluon
item_weapon_hivehand
item_weapon_mp5
item_weapon_rpg
item_weapon_satchel
item_weapon_shotgun
item_weapon_snark
item_weapon_tau
item_weapon_tripmine
...
weapon_357
weapon_assassin_glock
weapon_crossbow
weapon_crowbar
weapon_frag
weapon_glock
weapon_gluon
weapon_headcrab
weapon_hivehand
weapon_mp5
weapon_rpg
weapon_satchel
weapon_shotgun
weapon_snark
weapon_tau
weapon_tripmine
...
Image
User avatar
hugo5511
Junior Member
Posts: 25
Joined: Mon Jun 29, 2015 1:20 am

Postby hugo5511 » Mon Jul 06, 2015 12:30 am

Thanks for the info, i have copied the code snip from here http://forums.sourcepython.com/showthread.php?146-Stripping-weapons down the page a little.
I have noticed that bms will use weapon_mp5 or item_weapon_mp5 the sdk uses item_weapon_mp5.
I tried your script Stonedegg but i think i may of jumped the gun and it did not give any weaps.
I downloaded the 7/2/15 build of bms sp, should i wait until the recent update (7/5/15) gets moved over to a build ? Thanks Satoon 101 and Stonedegg for you help
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Jul 06, 2015 2:36 am

I believe stonedegg's last version should work if you remove the item_ prefix from all of the names. I haven't tested it, specifically, but it looks correct. Though, I also don't know what item_longjump is. That is not a weapon, so you might have that name correct. I will have to test that one out myself at some point.

You also will want to change your server's ../cfg/source-python/core_settings.ini to make sure that exceptions are printed to the console or log. I usually keep my 'level' setting in the LOG_SETTINGS section set to 2, so I can see all exceptions and warnings.

Once a new build is available, you will be able to use:

Syntax: Select all

from events import Event
from filters.weapons import WeaponClassIter
from players.entity import PlayerEntity
from players.helpers import index_from_userid


weapons = list(WeaponClassIter(return_types=['classname', 'maxammo']))


@Event
def player_spawn(game_event):
# Get the player's userid
userid = game_event.get_int('userid')

# Get the player's index
index = index_from_userid(userid)

# Get the player's PlayerEntity instance
player = PlayerEntity(index)

# Make sure the player isn't dead (player_spawn also fires when player_activate gets fired)
if player.isdead:
return

# Give the player the weapon
for weapon, maxammo in weapons:

# Check if the player has this weapon
if player.get_weapon_index(weapon):

# Don't do anything
continue

# Give the player the weapon
player.give_named_item(weapon, 0, maxammo, -1)
Image
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Mon Jul 06, 2015 10:32 am

Just a wild guess, but I think BMS uses the item_ prefix for entities that are lying around the map and respawning everytime, and just the weapon_ entities for weapons that are held by a player.
Means, once a player picks up an item_weapon_mp5 for example, it becomes weapon_mp5, so both should work when creating those entities. But I'm not sure about that.

item_longjump is equipment that you can pickup, it just lets you jump farther when you crouch jump.
User avatar
hugo5511
Junior Member
Posts: 25
Joined: Mon Jun 29, 2015 1:20 am

Postby hugo5511 » Thu Jul 09, 2015 10:56 pm

stonedegg i tested your script tonight without the item_ prefix and it has thrown this error

Syntax: Select all

[SP] Caught an Exception: 
Traceback (most recent call last):
File '..\addons\source-python\packages\source-python\events\listener.py', line 93, in fire_game_event
callback(game_event)
File '..\addons\source-python\plugins\giver\giver.py', line 45, in player_spawn
spawn_entity(entity.origin)

Boost.Python.ArgumentError: Python argument types in
_entities._helpers.spawn_entity(Vector)
did not match C++ signature:
spawn_entity(unsigned int entity_index)


I tried with "item_" prefix added like it was to start and got this error

Syntax: Select all

[SP] Caught an Exception: 
Traceback (most recent call last):
File '..\addons\source-python\packages\source-python\events\listener.py', line 93, in fire_game_event
callback(game_event)
File '..\addons\source-python\plugins\giver\giver.py', line 45, in player_spawn
spawn_entity(entity.origin)

Boost.Python.ArgumentError: Python argument types in
_entities._helpers.spawn_entity(Vector)
did not match C++ signature:
spawn_entity(unsigned int entity_index)

same error?

Hope this helps
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Jul 09, 2015 11:14 pm

Change "origin" in the last line to "index" (without the quotes).
Image
User avatar
hugo5511
Junior Member
Posts: 25
Joined: Mon Jun 29, 2015 1:20 am

Postby hugo5511 » Fri Jul 10, 2015 1:18 am

Whoo-hoo Work's perfect with no error's. Thank you very much stonedegg and satoon101.
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Fri Jul 10, 2015 11:48 am

No idea how i missed that :D
User avatar
hugo5511
Junior Member
Posts: 25
Joined: Mon Jun 29, 2015 1:20 am

Postby hugo5511 » Sun Jul 12, 2015 1:11 am

while testing tonight with more than one player i noticed this error in the console, with one player, it was not there,the script still does work and give the items as it should

Syntax: Select all

[SP] Caught an Exception: 
Traceback (most recent call last):
File '..\addons\source-python\packages\source-python\events\listener.py', line 93, in fire_game_event
callback(game_event)
File '..\addons\source-python\plugins\giver\giver.py', line 37, in player_spawn
if player.get_weapon_index(weapon):
File '..\addons\source-python\packages\source-python\players\weapons\__init__.py', line 304, in get_weapon_index
for index in self.weapon_indexes(classname, is_filters, not_filters):
File '..\addons\source-python\packages\source-python\players\weapons\__init__.py', line 341, in weapon_indexes
edict = Entity(index)
File '..\addons\source-python\packages\source-python\entities\entity.py', line 58, in __new__
'Index '{0}' is not a proper entity index.'.format(index))

ValueError: Index '275' is not a proper entity index.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Sun Jul 12, 2015 9:46 pm

hugo5511 wrote:while testing tonight with more than one player i noticed this error in the console, with one player, it was not there,the script still does work and give the items as it should

Syntax: Select all

[SP] Caught an Exception: 
Traceback (most recent call last):
File '..\addons\source-python\packages\source-python\events\listener.py', line 93, in fire_game_event
callback(game_event)
File '..\addons\source-python\plugins\giver\giver.py', line 37, in player_spawn
if player.get_weapon_index(weapon):
File '..\addons\source-python\packages\source-python\players\weapons\__init__.py', line 304, in get_weapon_index
for index in self.weapon_indexes(classname, is_filters, not_filters):
File '..\addons\source-python\packages\source-python\players\weapons\__init__.py', line 341, in weapon_indexes
edict = Entity(index)
File '..\addons\source-python\packages\source-python\entities\entity.py', line 58, in __new__
'Index '{0}' is not a proper entity index.'.format(index))

ValueError: Index '275' is not a proper entity index.


That is what actually happens to me too, not on BMS tho, but on CS:S. Is the issue known?
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Fri Sep 11, 2015 6:10 pm

I've found a solution:

Syntax: Select all

# Imports
from events import Event

from filters.entities import EntityIter

from players.entity import PlayerEntity
from players.helpers import index_from_userid


# Global Variables
# Create an EntityIter instance for convenience
weapon_indexes = EntityIter('weapon_', False, 'entity')


# Extend PlayerEntity with another method...
class Player(PlayerEntity):

# Simulate <PlayerEntity>.weapon_indexes()...
def weapons(self, classname=None, is_filters=None, not_filters=None):

if is_filters is not None and isinstance(is_filters, str):
is_filters = [is_filters]

if not_filters is not None and isinstance(not_filters, str):
not_filters = [not_filters]

# ... Loop through each weapon entity the player owns
for weapon in filter(lambda x: x.owner == self.inthandle, weapon_indexes):

# Are we filtering?
if classname is None and is_filters is None and not_filters is None:

# If not, yield the Entity instance
yield weapon

# If yes, get a prefixless classname, please
prefixless = weapon.classname.split('_')[1]

# Yield the items that match the given arguments...
if classname is not None and prefixless == classname:
yield weapon

if is_filters is not None and prefixless in is_filters:
yield weapon

if not_filters is not None and prefixless not in not_filters:
yield weapon


# -- Test --

@Event('player_say')
def on_say(game_event):

userid = game_event.get_int('userid')
index = index_from_userid(userid)

player = Player(index)

for weapon in player.weapons(is_filters='usp'):
print(weapon.classname)
Works with the latest release (September 7) - don't know if it suits you as well as <PlayerEntity>.weapon_indexes() .
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Sep 13, 2015 3:14 pm

A few things with that implementation. First, your weapon_indexes object is a bit misleading, since it iterates over Entity instances and not indexes. Also, using only the weapon_ prefix would not work on all games. For instance, TF2 uses tf_weapon_ as the prefix. The weapon_manager dictionary is setup to handle prefixes already. With your version, it is possible for a weapon to be iterated over more than once. I also don't like the idea of using classnames as filters (ie is_filters='usp' when usp is part of a classname).

The main reason I prefer fixing the current implementation to not encounter those errors over the idea of the implementation you have shown is that your's iterates over 2048 entity indexes every time, while the current implementation only iterates over the length of m_hMyWeapons. For CS:S (and most other games), that is only 48 items (64 for CS:GO). I would really like to figure out why those errors are occurring, but I have yet to be able to find reproducible code/situation that replicates the error consistently.
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Sun Sep 13, 2015 9:16 pm

satoon101 wrote:A few things with that implementation. First, your weapon_indexes object is a bit misleading, since it iterates over Entity instances and not indexes. Also, using only the weapon_ prefix would not work on all games. For instance, TF2 uses tf_weapon_ as the prefix. The weapon_manager dictionary is setup to handle prefixes already. With your version, it is possible for a weapon to be iterated over more than once. I also don't like the idea of using classnames as filters (ie is_filters='usp' when usp is part of a classname).

The main reason I prefer fixing the current implementation to not encounter those errors over the idea of the implementation you have shown is that your's iterates over 2048 entity indexes every time, while the current implementation only iterates over the length of m_hMyWeapons. For CS:S (and most other games), that is only 48 items (64 for CS:GO).

Ooops, you're definitely right, I didn't know the internals of each function. Tho, for now, 2048 lookups and no error is better than 48 lookups and errors imo :)

satoon101 wrote:I would really like to figure out why those errors are occurring, but I have yet to be able to find reproducible code/situation that replicates the error consistently.

Well, sometimes it happens, and sometimes not. Maybe a Valve update caused this?
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Sep 13, 2015 9:48 pm

It's definitely not a Valve update. CS:S hasn't been updated since a year or more. However, PlayerEntity.weapon_indexes() has been updated on the 25th July and I see that you are using a newer version of SP (you are using the new event decorator). So, you should definitely get a different error message that probably tells us which conversion failed. Could you please try out the latest SP build (just to be sure)?

Return to “Plugin Requests”

Who is online

Users browsing this forum: No registered users and 34 guests