Playerlib.Player

From EventScripts Community Encyclopedia

Contents

Class: playerlib.Player

Module: playerlib
EventScripts version: 2.0.0.250g

Overview

Description

Allows getting and setting of player properties as well as frequently used player functions.

Quick Example

""" Attackers leech health from players hurt """
import playerlib
 
def player_hurt(event_var):
   attacker = int(event_var['attacker'])
   # We can only proceed if there was an attacker
   if attacker:
      # Get our playerlib player
      player = playerlib.getPlayer(attacker)
      # Add the damage caused to the victim to the attacker's health
      player.health += int(event_var['dmg_health'])
 
""" Set players who attack teammates on fire """
import playerlib
 
def player_hurt(event_var):
   # We only burn players who attack teammates
   if event_var['es_userteam'] == event_var['es_attackerteam']:
      attacker = event_var['attacker']
      # If the player hurt him- or her- self we won't burn the player
      if event_var['userid'] != attacker:
         # This must be another teammate attacking the player, so burn the teammate
         player = playerlib.getPlayer(attacker)
         player.burn()
 
""" Every time a player jumps show the player's primary weapon """
import playerlib
 
def player_jump(event_var):
   # Get our playerlib player
   player  = playerlib.getPlayer(event_var['userid'])
   # Get the player's primary weapon (as given by weaponlib)
   primary = player.primary
   # If the player has no primary weapon we have nothing to show
   if primary:
      es.msg('Player ' + event_var['es_username'] + ' has ' + primary + ' as a primary weapon.')
 
"""
Every time an attacker hurts another player with a secondary weapon
the attacker gains a bullet in reserve ammo for the secondary weapon.
"""
import playerlib
import weaponlib
 
def player_hurt(event_var):
   attacker = int(event_var['attacker'])
   # Again, we need to make sure the player was hurt by another player
   if attacker:
      # With weaponlib we can determine if the weapon used is a secondary weapon
      weapon = weaponlib.getWeapon(event_var['weapon'])
      # Only if weaponlib recognizes the weapon can we determine the slot of the weapon
      if weapon and weapon.slot == 2:
         player = playerlib.getPlayer(attacker).ammo.secondary += 1

Classes

Methods

Get methods

getPrimary()
Returns the player's primary weapon. If no corresponding weapon is found a ReturnValue instance equivalent to False will be returned.
getSecondary()
Returns the player's scondary weapon. If no corresponding weapon is found a ReturnValue instance equivalent to False will be returned.
getHE()
Returns the number of HE grenades the player has
getFB()
Returns the number of flashbangs the player has
getSG()
Returns the number of smoke grenades the player has
hasC4()
Returns a ReturnValue instance equivalent to True if the player has C4 otherwise the instance will be equivalent to False
getAmmo(weapon)
Returns the player's ammo for the specified weapon. weapon accepts single weapon names, primary or 1, and secondary or 2. If no corresponding weapon is found a ReturnValue instance equivalent to False will be returned.
getClip(weapon)
Returns the number of bullets in the player's specified gun. weapon accepts single weapon names, primary or 1, and secondary or 2. If no corresponding weapon is found a ReturnValue instance equivalent to False will be returned.
getWeaponIndex(weapon)
Returns the index of the specified weapon name the player is holding. If the player is not holding the weapon zero is returned.
getWeaponList()
Returns a list of the names of weapons the player has. This function only counts entities so some weapons may not appear and grenades only appear once if present.
getViewAngle()
Returns the player's view angle. The output should be the same as with est_getviewangle.
getCash()
Returns the amount of money the player has
getHealth()
Returns the amount of health the player has
getArmor()
Returns the amount of armor the player has
getSpeed()
Returns the speed modifier for the player's speed
hasDefuser()
Returns one if the player has a defuse kit otherwise returns zero
viewVector()
Returns the player's view vector
getDistance(value, plane='xyz')
Returns the player's distance to a player or coordinate on the given plane. value parameter can either be a three-element iterable of coordinates or an userid.
getNoClip()
Returns a ReturnValue instance equivalent to True if the player has noclip enabled otherwise the instance will be equivalent to False
getNoBlock()
Returns a ReturnValue instance equivalent to True if the player has noblock enabled otherwise the instance will be equivalent to False
getJetpack()
Returns a ReturnValue instance equivalent to True if the player has jetpack enabled otherwise the instance will be equivalent to False
getFreeze()
Returns a ReturnValue instance equivalent to True if the player is frozen otherwise the instance will be equivalent to False
getLocation()
Returns a three-element tuple with the player's coordinates, equivalent to es.getplayerlocation
getFlashAlpha()
Returns the player's flash alpha value (0 to 255)
getFlashDuration()
Returns the player's flash duration in seconds
getFlash()
Returns a two-element tuple containing (getFlashAlpha(), getFlashDuration())
getColor()
Returns a four-element tuple with the player's color in RGBA format
getLanguage()
Returns the player's language for use with langlib
isDucked()
Returns one if the player is ducked otherwise returns zero
hasNightvision()
Returns one if the player has night vision otherwise returns zero
getNightvisionState() or getNightvisionOn()
Returns one if the player's night vision is activated otherwise returns zero
inBuyZone()
Returns one if the player is in a buy zone otherwise returns zero
inBombZone()
Returns one if the player is in a bomb zone otherwise returns zero
inRescueZone()
Returns one if the player is in a hostage rescue zone otherwise returns zero
isDefusing()
Returns one if the player is defusing the bomb otherwise returns zero
hasHelmet()
Returns one if the player has a helmet otherwise returns zero
onGround()
Returns one if the player's feet are on the ground (not jumping, swimming, etc) otherwise returns zero
inGodMode()
Returns one if the player is in god mode otherwise returns zero
getWeaponColor()
Returns a four-element tuple of the color in RGBA format of the player's active weapon
getViewCoord()
Returns the coordinates where the player's aiming reticle is pointing
getEyeLocation()
Returns the coordinates of the player's eye location
isObstructionBetween(value)
Returns True if there is an obstruction (wall, box, tree, etc) between the player and the coordinates or userid passed as the value parameter, otherwise returns False
getNearPlayers(distance)
Returns a list of userids of players within the specified distance
getClosestPlayer(team=None)
Returns a two part tuple, the userid of the closest player and the distance. If team is provided only players on the given team will be checked.
uniqueid(botname=False)
Returns a unique ID for the player. If botname is True bots will have an unique ID based on their name instead of the default BOT. Visit the ESS uniqueid page for more information: http://www.eventscripts.com/pages/Uniqueid
getAttribute(attribute)
Returns an attribute for the player retrieved from es.createplayerlist
get(param, passparam="")
Returns any playerlib.Player attribute that can be read. If a sub-attribute is required it can be passed to the passparam keyword.

Set methods

setAmmo(weapon, value)
Sets the ammo for the specified weapon or raises ValueError if the weapon is invalid
setPrimaryAmmo(value)
Sets the ammo for the player's primary weapon or raises KeyError if the player has no primary weapon
setSecondaryAmmo(value)
Sets the ammo for the player's secondary weapon or raises KeyError if the player has no secondary weapon
setClip(weapon, value)
Sets the clip amount for the specified weapon, raises ValueError if the weapon is invalid, or raises KeyError if the player doesn't have the specified weapon
setPrimaryClip(value)
Sets the clip ammo for the player's primary weapon or raises KeyError if the player has no primary weapon
setSecondaryClip(value)
Sets the clip ammo for the player's secondary weapon or raises KeyError if the player has no secondary weapon
setHealth(value)
Sets the player's health amount
setArmor(value)
Sets the player's armor amount
setSpeed(value)
Sets the player's speed multiplier
setCash(value)
Sets the player's cash amount
setDefuser(value)
Set to one to give the player a defuse kit or set to zero to remove the player's defuse kit
setNightvision(value)
Set to one to give the player a night vision or set to zero to remove the player's night vision
setNightvisionState(value) or setNightvisionOn(value)
Set to one to toggle the player's night vision on or set to zero to toggle the player's night vision off
viewPlayer(value)
Sets the player's view to look at another player
viewCoord(value)
Sets the player's view to look at a specified coordinate
lookAt(value)
When given a userid this function calls viewPlayer, when given a coordinate this function calls viewCoord
push(horiz, vert, vert_override=False)
Pushes the player along his or her view vector. See the ESS playerset push for more information.
setModel(value)
Sets the player's model when given a model name. Precaching is done automatically.
setColor(r, g, b, a=None)
Sets the player's color with RGBA format. When the alpha value is omitted the player's current alpha is preserved.
noblock(value)
Set to one to trigger noblock for the player or set to zero to remove noblock
jetpack(value)
Set to one to trigger jetpack for the player or set to zero to remove jetpack
freeze(value)
Set to one to freeze the player or set to zero to unfreeze the player
godmode(value)
Set to one to set the player in god mode or set to zero to remove god mode
setLocation(value)
Sets the player's location to a coordinate in a three-element storage type
    • Warning: This command uses es.server.cmd with es_setpos to set the player's location. This method has been reported to crash some servers. You can instead use es.server.queuecmd with es_setpos if you want to trade immediate relocation for stability.
moveTo(value)
Moves the player to be near another player as specified by userid
flash(alpha, duration) or blind(alpha, duration)
Blinds the player as if he or she were hit by a flashbang
setHE(value)
Sets the player's HE grenade count
setFB(value)
Sets the player's flashbang count
setSG(value)
Sets the player's smoke grenade count
setWeaponColor(r, g, b, a=None)
Sets the player's active weapon to a color in RGBA format. When alpha value is omitted the weapon's current alpha value is used. If the player changes his or her active weapon this function will need to be called again if you want the new weapon to be colored.
burn()
Sets the player on fire
extinguish()
Extinguishes a player on fire
set(param, value)
Sets any playerlib.Player attribute that can be written to the specified value. If a sub-attribute is required the value parameter must be a tuple with the sub-attribute as the first element.

Miscellaneous methods

isOnline() or getOnline()
Returns one if the player is online otherwise returns zero
kill() or slay()
Slays the player using es.sexec and the kill command
kick(reason=None)
Kicks the player with a reason, if provided
banId(time, kick=False, reason=None)
Bans the player by Steam ID for the specified time. If kick is True then kick will be called with the specified reason, if provided.
banAddr(time, kick=False, reason=None)
Bans the player by IP for the specified time. If kick is True then kick will be called with the specified reason, if provided.
add(param, value)
Adds a value to certain player properties including ammo, clip, health, armor, speed, and cash. If a sub-property is required the value parameter must be a tuple with the sub-property as the first element, e.g. myPlayer.add('clip', ('secondary' 12)).
    • Deprecated in favor of attribute math, e.g. myPlayer.add('health', 10) is equivalent to myPlayer.health += 10.

Attributes

  • All attributes are also items similar to items in a dictionary, e.g. myPlayer['health'] = 100 is equivalent to myPlayer.health = 100. The advantage of this is items can take integer or string values whereas attribute names can only be strings and are commonly hard-coded.
  • Many readable attributes also have documentation on the ESS playerget page.
  • Many writable attributes also have documentation on the ESS playerset page.

Read-only attributes

primary
Equivalent to getPrimary
secondary
Equivalent to getSecondary
c4
Equivalent to hasC4
weaponindex
Equivalent to getWeaponIndex
weaponlist
Equivalent to getWeaponList
viewangle
Equivalent to getViewAngle
viewvector
Equivalent to getViewVector
distance
Equivalent to getDistance. This attribute needs a sub-item containing [value, plane]. Plane is optional and can be omitted; if omitted it will default to xyz.
flashalpha
Equivalent to getFlashAlpha
lang
Equivalent to getLanguage
isducked
Equivalent to isDucked
inbuyzone
Equivalent to inBuyZone
inbombzone
Equivalent to inBombZone
inrescuezone
Equivalent to inRescueZone
isdefusing
Equivalent to isDefusing
onground
Equivalent to onGround
viewcoord
Equivalent to getViewCoord
eyelocation
Equivalent to getEyeLocation
obstructionbetween
Equivalent to isObstructionBetween. This attribute needs a sub-item containing the coordinates or userid to detect if there is an obstruction between the end point and the player.
nearplayer or nearplayers
Equivalent to getNearPlayers. This attribute needs a sub-item containing the distance from the player to check.
closestplayer or closestplayerbyteam
Equivalent to getClosestPlayer. Team to check can be specified as a sub-item to this attribute.
uniqueid
Equivalent to the uniqueid function
userid
Returns the player's userid
team
Returns the player's team number
  • All attributes from es.createplayerlist are also available as read-only attributes (unless listed elsewhere). In CSS the current attributes are as follows:
name
Returns the player's name
steamid
Returns the player's Steam ID
index
Returns the player's entity index
teamid
Returns the player's team number
kills
Returns the player's kill count
deaths
Returns the player's death count
model
Returns the name of the player's model (default models only)
isdead
Returns one if the player is dead otherwise returns zero
isbot
Returns one if the player is a bot otherwise returns zero
isobserver
Returns one if the player is an observer otherwise returns zero
ishltv
Returns one if the player is a SourceTV client otherwise returns zero
isinavehicle
Returns one if the player is in a vehicle otherwise returns zero
serialnumber
Returns the player's serial number (a combination of the player's index and an increasing serial number value)
weapon
Returns the player's active weapon
handle
Returns the player's handle
ping
Returns the player's ping
packetloss
Returns the number of packets sent to the player that were lost
timeconnected
Returns the number of seconds the player has been connected
address
Returns the IP address with port of the player
language
Returns the player's language as determined by the player's cl_language value. This language string is not compatible with langlib, use the lang attribute to get a langlib-compatible language value.
x
Returns the player's x value on the map
y
Returns the player's y value on the map
z
Returns the player's z value on the map

Write-only attributes

viewplayer
Equivalent to viewPlayer
viewcoord
Equivalent to viewCoord
lookat
Equivalent to the lookAt function
push
Equivalent to the push function
model
Equivalent to setModel
moveto
Equivalent to the moveTo function
burn
Equivalent to the burn function. Anything this attribute is set to will light the player on fire.
extinguish
Equivalent to the extinguish function. Anything this attribute is set to will extinguish the player.

Readable and writable attributes

  • Most attributes that can be both read and written to can perform attribute math (when practical), e.g. myPlayer.health += 25 or myPlayer.clip.secondary += 12.
health
Equivalent to getHealth or setHealth
armor
Equivalent to getArmor or setArmor
cash
Equivalent to getCash or setCash
speed
Equivalent to getSpeed or setSpeed
ammo
Equivalent to getAmmo or setAmmo. This attribute needs a sub-attribute that can be primary, secondary, or a weapon name.
clip
Equivalent to getClip or setClip. This attribute needs a sub-attribute that can be primary, secondary, or a weapon name.
he
Equivalent to getHE or setHE
fb
Equivalent to getFB or setFB
sg
Equivalent to getSG or setSG
defuser
Equivalent to hasDefuser or setDefuser
noclip
Equivalent to getNoClip or the noclip function
noblock
Equivalent to getNoBlock or the noblock function
jetpack
Equivalent to getJetpack or the jetpack function
freeze
Equivalent to getFreeze or the freeze function
godmode
Equivalent to inGodMode or the godmode function
flash
Equivalent to getFlash or the flash function
color
Equivalent to getColor or setColor
weaponcolor
Equivalent to getWeaponColor or setWeaponColor
nightvision
Equivalent to getNightvision or setNightvision
nightvisionstate or nightvisionon
Equivalent to getNightvisionState or setNightvisionState
location
Equivalent to getLocation or setLocation
viewcoord
Equivalent to getViewCoord or viewCoord

Notes

See Also

blog comments powered by Disqus