Player, Weapon entity, and Weapon type Iterators

Post Python examples to help other users.
User avatar
satoon101
Project Leader
Posts: 2384
Joined: Sat Jul 07, 2012 1:59 am

Player, Weapon entity, and Weapon type Iterators

Postby satoon101 » Sun Jan 20, 2013 1:54 am

Currently, there are 3 iterators built into Source.Python:
  • PlayerIter - iterates over players on the server
  • WeaponIter - iterates over weapon entities on the server
  • WeaponTagIter - iterates over weapons in the WeaponManager (not actual weapon entities on the server)

All 3 iterators, and all iterators for the forseeible future, will have 3 arguments:
  1. IS filters (is_filters)
  2. NOT filters (not_filters)
  3. Return Types (return_types)

Each of the three arguments can be either a single string or a list of strings. Current values for each are as follows:
  • PlayerIter:
    • is_filters/not_filters:
      • all
      • bot
      • human
      • alive
      • dead
      • ct
      • t
      • un
      • spec
      • <also any other team tag depending on the game>
    • return_types:
      • index
      • edict
      • basehandle
      • inthandle
      • pointer
      • userid
      • uniqueid
      • address
      • info - IPlayerInfo instance for the player
      • player - PlayerEntity instance for the player
      • name
      • steamid
      • location - Vector instance for the player's location
      • kills
      • deaths
      • model
      • health
      • armor
      • weapon - classname of the weapon the player is currently holding
      • language
      • team
  • WeaponIter:
    • is_filters/not_filters:
      • <any filters that are found in the game's weapon data ini file>
      • Examples:
        • primary
        • secondary
        • melee
    • return_types:
      • index
      • edict
      • basehandle
      • inthandle
      • pointer
      • weapon - WeaponEntity instance for the weapon
  • WeaponTagIter:
    • is_filters/not_filters:
        <any filters that are found in the game's weapon data ini file>
      • Examples:
        • primary
        • secondary
        • melee
    • return_types:
      • weapon - WeaponManager values for the weapon type
      • classname - the actual classname of the weapon type

If you do not pass any parameters, there will be no is/not filters, so all values will be iterated over. The default values for return_types are:
  • PlayerIter - index
  • WeaponIter - index
  • WeaponTagIter - weapon

Remember that these are iterators. The best way, typically, to use them, is to store an instance, and then just iterate over it when you need to:

Syntax: Select all

from filters.players import PlayerIter

# This stores an generator for alive player "indexes"
alive_players = PlayerIter('alive')

# Later in the script, use
for index in alive_players:


For a more complex example, that shows all 3 arguments:

Syntax: Select all

from filters.players import PlayerIter

# Store a generator for dead cts and dead ts for their uniqueid, name, and location
dead_on_team_players = PlayerIter('dead', ['un', 'spec'], ['uniqueid', 'name', 'location'])

# Then, to use it later in the script
for uniqueid, name, location in dead_on_team_players:
print('%s has a uniqueid of %s and is located at "%s %s %s"' % (name, uniqueid, location.x, location.y, location.z))


Sometimes, you might want to store information instead of iterating over it later. This is especially the case with WeaponTagIter, as it does not deal with actual entities on the server, so its information will never change. To store a list, instead of iterating, simply use:

Syntax: Select all

from filters.weapontags import WeaponTagIter

_primary_weapon_classnames = list(WeaponTagIter('primary', return_types='classname'))

_non_sniper_assault_rifle_classnames = list(WeaponTagIter('rifle', 'sniper', 'classname'))


If you see anything missing, or wish for a further explanation, please feel free to post.

Thanks,
Satoon

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest