Gungame51.core.players.Player

From GunGame5 Documentation

(Difference between revisions)
Jump to: navigation, search
 
(19 intermediate revisions not shown)
Line 8: Line 8:
ggversion=5.1|
ggversion=5.1|
-
description=|
+
description=This class is intended to be used as the class container for interaction with all GunGame-based player attributes. This class forwards to the stored [[gungame.core.players.PlayerDict|PlayerDict]] instance of the player's userid, which returns the stored player's instance of the [[gungame.core.players.BasePlayer|BasePlayer]] class. The use of this class eases the process of interacting with players, as opposed to referring to the [[gungame.core.players.PlayerDict|PlayerDict's]] instance containing the [[gungame.core.players.BasePlayer|BasePlayer]] instance of the player directly.|
-
shortexample=|
+
shortexample=* Accessing attributes from [[gungame.core.players.BasePlayer#Attributes|BasePlayer]]:
 +
<source lang="python">import es
 +
 
 +
from gungame.core.players import Player
 +
 
 +
def player_spawn(event_var):
 +
    userid = event_var['userid']
 +
 
 +
    # When a player spawns, echo their attributes to console
 +
    # Below, we will use multiple methods to accomplish retrieving the
 +
    # player's attributes.
 +
 
 +
    # Get the Player() object --- Player.__init__(userid) --- requires a userid
 +
    myPlayer = Player(userid)
 +
 
 +
    # Echo the attributes
 +
    es.dbgmsg(0, 'Player Userid: %s' %myPlayer.userid)
 +
    es.dbgmsg(0, 'Player Level: %s' %Player(userid).level)
 +
    es.dbgmsg(0, 'Player Multikill: %s' %Player(userid)['multikill'])
 +
    es.dbgmsg(0, 'Player Steamid: %s' %myPlayer['steamid'])
 +
    es.dbgmsg(0, 'Player Weapon: %s' %myPlayer.weapon)</source>
 +
 
 +
* Accessing methods from [[gungame.core.players.BasePlayer#Methods|BasePlayer]]:
 +
<source lang="python">import es
 +
 +
from gungame.core.players import Player
 +
 +
def player_spawn(event_var):
 +
    userid = event_var['userid']
 +
 
 +
    # Get the Player() object --- Player.__init__(userid) --- requires a userid
 +
    myPlayer = Player(userid)
 +
 
 +
    # When a player spawns, strip their weapon and give them their current
 +
    # level's weapon as well as message the player telling them what weapon
 +
    # we just gave them.
 +
    myPlayer.strip()
 +
    myPlayer.giveWeapon()
 +
    es.tell(userid, 'We just gave you the "%s"!' %myPlayer.getWeapon())</source>
 +
 
 +
* Using the static class methods:
 +
<source lang="python"># ../<MOD>/addons/eventscripts/gungame/scripts/custom/gg_sample/gg_sample.py
 +
 +
from gungame.core.players.shortcuts import setAttribute
 +
from gungame.core.players import Player
 +
 +
def load():
 +
    # Set the custom attribute "myattribute" on all connected players
 +
    setAttribute('#all', 'myattribute', 0)
 +
 +
    # Add the attribute callback
 +
    # (this will call on "callbackFunction" any time "myattribute" is set
 +
    #  via the __setitem__ or __setattr__ method)
 +
    Player.addAttributeCallBack('myattribute', callbackFunction, 'gg_sample')
 +
 +
def unload():
 +
    '''
 +
    When you unload the custom addon, so long as the correct "addon" argument
 +
    was provided, "removeCallBackForAddons('addon_name_being_unloaded')" will be called,
 +
    which in this case will be "removeCallBackForAddon('gg_sample')". Therefore, the
 +
    following "removeAttributeCallBack('myattribute')" and "removeCallBacksForAddon('gg_sample')" is not necessary, but moreso exists for the sake of showing how to use
 +
    the function.
 +
    '''
 +
    # Remove the custom attribute "myattribute" from all players
 +
    Player.removeAttributeCallBack('myattribute')
 +
 +
    # Remove all custom attribute callbacks for this addon
 +
    Player.removeCallBacksForAddon('gg_sample')
 +
 +
def callbackFunction(name_of_the_attribute, value_to_be_checked):
 +
    '''
 +
    This callback function can be used for more than 1 attribute, so that you can
 +
    register multiple attribute callbacks. All that you have to do is check the
 +
    name of the attribute (the first argument). The second argument is the value
 +
    of the attribute. If you raise an error during your callback, the attribute
 +
    will not be set to the attempted value.
 +
    '''
 +
    if name_of_the_attribute == 'myattribute':
 +
        # Make sure the value is from 0-10
 +
        if value_to_be_checked in range(0, 11):
 +
            pass
 +
        else:
 +
            raise ValueError('Value must be between 0 and 10!')</source>|
methods=* [[gungame.core.players.Player.__init__|__init__]](''userid'')|
methods=* [[gungame.core.players.Player.__init__|__init__]](''userid'')|
Line 17: Line 99:
staticmethods=* [[gungame.core.players.Player.addAttributeCallBack|addAttributeCallBack]](''attribute, function, addon'')
staticmethods=* [[gungame.core.players.Player.addAttributeCallBack|addAttributeCallBack]](''attribute, function, addon'')
* [[gungame.core.players.Player.removeAttributeCallBack|removeAttributeCallBack]](''attribute'')
* [[gungame.core.players.Player.removeAttributeCallBack|removeAttributeCallBack]](''attribute'')
-
* [[gungame.core.players.Player.removeCallBacksForAddon|removeCallBacksForAddon]](''addon'')|
+
* [[gungame.core.players.Player.removeCallBacksForAddon|removeCallBacksForAddon]](''addon'')
 +
 
 +
This class allows you to access class methods from the [[gungame.core.players.BasePlayer#Methods|BasePlayer]] class. See the [[gungame.core.players.BasePlayer#Methods|BasePlayer]] class for a list of methods that can be used with the [[gungame.core.players.Player|Player()]] class container.|
 +
 
 +
attributes=* userid
-
attributes=|
+
This class allows you to both get and set attributes existing and custom attributes to the [[gungame.core.players.BasePlayer#Attributes|BasePlayer]] class. See the [[gungame.core.players.BasePlayer#Attributes|BasePlayer]] class for a list of attributes that can be used with the [[gungame.core.players.Player|Player()]] class container.|
notes=|
notes=|

Current revision as of 18:30, 14 November 2011



Contents

Class: Player

Module: gungame.core.players
GunGame Version: 5.1

Overview

Description

This class is intended to be used as the class container for interaction with all GunGame-based player attributes. This class forwards to the stored PlayerDict instance of the player's userid, which returns the stored player's instance of the BasePlayer class. The use of this class eases the process of interacting with players, as opposed to referring to the PlayerDict's instance containing the BasePlayer instance of the player directly.

Quick Example

import es
 
from gungame.core.players import Player
 
def player_spawn(event_var):
    userid = event_var['userid']
 
    # When a player spawns, echo their attributes to console
    # Below, we will use multiple methods to accomplish retrieving the
    # player's attributes.
 
    # Get the Player() object --- Player.__init__(userid) --- requires a userid
    myPlayer = Player(userid)
 
    # Echo the attributes
    es.dbgmsg(0, 'Player Userid: %s' %myPlayer.userid)
    es.dbgmsg(0, 'Player Level: %s' %Player(userid).level)
    es.dbgmsg(0, 'Player Multikill: %s' %Player(userid)['multikill'])
    es.dbgmsg(0, 'Player Steamid: %s' %myPlayer['steamid'])
    es.dbgmsg(0, 'Player Weapon: %s' %myPlayer.weapon)
import es
 
from gungame.core.players import Player
 
def player_spawn(event_var):
    userid = event_var['userid']
 
    # Get the Player() object --- Player.__init__(userid) --- requires a userid
    myPlayer = Player(userid)
 
    # When a player spawns, strip their weapon and give them their current
    # level's weapon as well as message the player telling them what weapon
    # we just gave them.
    myPlayer.strip()
    myPlayer.giveWeapon()
    es.tell(userid, 'We just gave you the "%s"!' %myPlayer.getWeapon())
  • Using the static class methods:
# ../<MOD>/addons/eventscripts/gungame/scripts/custom/gg_sample/gg_sample.py
 
from gungame.core.players.shortcuts import setAttribute
from gungame.core.players import Player
 
def load():
    # Set the custom attribute "myattribute" on all connected players
    setAttribute('#all', 'myattribute', 0)
 
    # Add the attribute callback
    # (this will call on "callbackFunction" any time "myattribute" is set
    #  via the __setitem__ or __setattr__ method)
    Player.addAttributeCallBack('myattribute', callbackFunction, 'gg_sample')
 
def unload():
    '''
    When you unload the custom addon, so long as the correct "addon" argument
    was provided, "removeCallBackForAddons('addon_name_being_unloaded')" will be called,
    which in this case will be "removeCallBackForAddon('gg_sample')". Therefore, the
    following "removeAttributeCallBack('myattribute')" and "removeCallBacksForAddon('gg_sample')" is not necessary, but moreso exists for the sake of showing how to use
    the function.
    '''
    # Remove the custom attribute "myattribute" from all players
    Player.removeAttributeCallBack('myattribute')
 
    # Remove all custom attribute callbacks for this addon
    Player.removeCallBacksForAddon('gg_sample')
 
def callbackFunction(name_of_the_attribute, value_to_be_checked):
    '''
    This callback function can be used for more than 1 attribute, so that you can
    register multiple attribute callbacks. All that you have to do is check the
    name of the attribute (the first argument). The second argument is the value
    of the attribute. If you raise an error during your callback, the attribute
    will not be set to the attempted value.
    '''
    if name_of_the_attribute == 'myattribute':
        # Make sure the value is from 0-10
        if value_to_be_checked in range(0, 11):
            pass
        else:
            raise ValueError('Value must be between 0 and 10!')

Methods

Static Methods

This class allows you to access class methods from the BasePlayer class. See the BasePlayer class for a list of methods that can be used with the Player() class container.

Attributes

  • userid

This class allows you to both get and set attributes existing and custom attributes to the BasePlayer class. See the BasePlayer class for a list of attributes that can be used with the Player() class container.

Notes

See Also

Personal tools