Gungame.core.players.shortcuts.setAttribute

From GunGame5 Documentation

(Difference between revisions)
Jump to: navigation, search
 
(31 intermediate revisions not shown)
Line 1: Line 1:
-
{{Function|name=setAttribute|
+
{{Function|
 +
 
 +
name=setAttribute|
 +
 
 +
 
module=gungame.core.players.shortcuts|
module=gungame.core.players.shortcuts|
 +
 +
class=[[gungame.core.players.Player|Player]]|
class=[[gungame.core.players.Player|Player]]|
-
version=|
+
 
 +
 
 +
ggversion=5.1|
 +
 
 +
 
 +
description=A shortcut function for [[gungame.core.players.Player|Player.__setattr__()]] and [[gungame.core.players.Player|Player.__setitem__()]] that allows the scripter to set a custom [[gungame.core.players.Player|Player]] attribute on a single userid or multiple userids using a filter as provided by [http://python.eventscripts.com/pages/Playerlib#Notes playerlib.getPlayerList()].|
arguments=<source lang="python">setAttribute(filter, attribute, value)</source>
arguments=<source lang="python">setAttribute(filter, attribute, value)</source>
-
* filter
+
* '''filter'''
-
** userid
+
** ''userid''
-
** A str() filter as used by [http://python.eventscripts.com/pages/Playerlib#Notes playerlib.getPlayerList()]
+
** ''A str() filter as used by [http://python.eventscripts.com/pages/Playerlib#Notes playerlib.getPlayerList()]''
-
* attribute
+
* '''attribute'''
-
** The str() name of the attribute
+
** ''The str() name of the attribute''
-
* value
+
* '''value'''
-
** The value of the attribute|
+
** ''The value of the attribute''|
-
examples=<source lang="python"># ../<MOD>/addons/eventscripts/gungame/scripts/custom/gg_sample/gg_sample.py
+
 
 +
 
 +
examples=* Setting the attribute:
 +
<source lang="python"># ../<MOD>/addons/eventscripts/gungame/scripts/custom/gg_sample/gg_sample.py
 +
 
 +
import es
from gungame.core.players.shortcuts import setAttribute
from gungame.core.players.shortcuts import setAttribute
-
from gungame.core.players.shortcuts import addAttributeCallBack
+
from gungame.core.players.shortcuts import deleteAttribute
-
from gungame.core.players.shortcuts import removeAttributeCallBack
+
-
from gungame.core.players.shortcuts import removeCallBacksForAddon
+
def load():
def load():
     # Set the custom attribute "myattribute" on all connected players
     # Set the custom attribute "myattribute" on all connected players
     setAttribute('#all', 'myattribute', 0)
     setAttribute('#all', 'myattribute', 0)
-
 
-
    # Add the attribute callback
 
-
    # (this will call on "callbackFunction" any time "myattribute" is set
 
-
    #  via the __setitem__ or __setattr__ method)
 
-
    addAttributeCallBack('myattribute', callbackFunction, 'gg_sample')
 
def unload():
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
     # Remove the custom attribute "myattribute" from all players
-
     removeAttributeCallBack('myattribute')
+
     deleteAttribute('#all', 'myattribute')
 +
 
 +
def gg_start():
 +
    # Set the custom attribute "myattribute" on all connected players
 +
    # (due to the fact that all custom player attributes are removed
 +
    # when the GunGame match ends, we need to re-instantiated the
 +
    # custom attribute "myattribute" when a new GunGame round starts)
 +
    setAttribute('#all', 'myattribute', 0)
 +
 
 +
def player_activate(event_var):
 +
    # Set the custom attribute "myattribute" for this userid
 +
    setAttribute(event_var['userid'], 'myattribute', 0)
 +
 
 +
def player_disconnect(event_var):
 +
    # Delete the custom attribute "myattribute" from this userid
 +
    deleteAttribute(event_var['userid'], 'myattribute')</source>
 +
 
 +
* Accessing the attribute:
 +
<source lang="python">import es
 +
 
 +
from gungame.core.players.shortcuts import Player
 +
 
 +
def player_say(event_var):
 +
    if event_var['text'] == 'myattribute':
 +
        userid = event_var['userid']
 +
 
 +
        # Using the Player.__getattr__() method
 +
        es.tell(userid, 'Your attribute: %s' %Player(userid).myattribute
 +
 
 +
        # Using the Player.__getitem__() method
 +
        es.tell(userid, 'Your attribute: %s' %Player(userid)['myattribute']
 +
</source>|
 +
 
 +
 
 +
notes=* In order to effectively set a custom attribute on all players, use the [[gungame.core.players.shortcuts.setAttribute|setAttribute()]] function in your addon's [http://www.eventscripts.com/pages/Block_load load()], [http://www.eventscripts.com/pages/Player_activate player_activate()], and gg_start() functions:
 +
<source lang="python">def load():
 +
    setAttribute('#all', 'yourattributename', 0)
 +
 
 +
def player_activate(event_var):
 +
    setAttribute(event_var['userid'], 'yourattributename', 0)
 +
 
 +
def gg_start():
 +
    setAttribute(event_var['userid'], 'yourattributename', 0)</source>
 +
* If you set any custom [[gungame.core.players.Player|Player]] attributes, insure that you remove them when the player disconnects (optional) or when your addon is unloaded (required). This can be accomplished via the [[gungame.core.players.shortcuts.deleteAttribute|deleteAttribute()]] function.
 +
* Once a custom [[gungame.core.players.Player|Player]] attribute has been set, you can access the attribute via the [[gungame.core.players.Player|Player.__getitem__()]] or  [[gungame.core.players.Player|Player.__getattr__()]] methods.
 +
** Other scripts can also access your custom attributes via the [[gungame.core.players.Player|Player.__getitem__()]] or  [[gungame.core.players.Player|Player.__getattr__()]] methods.
 +
* When GunGame starts a new GunGame round, all custom [[gungame.core.players.Player|Player]] attributes are deleted, and need to be re-instantiated. If you use the [http://www.eventscripts.com/pages/Block_load load()], [http://www.eventscripts.com/pages/Player_activate player_activate()], and gg_start() functions to set custom [[gungame.core.players.Player|Player]] attributes in your script, you will not have to worry about the custom attributes being inaccessible.|
-
    # Remove all custom attribute callbacks for this addon
 
-
    removeCallBacksForAddon('gg_sample')
 
-
def callbackFunction(name_of_the_attribute, value_to_be_checked):
+
seealso=* [[gungame.core.players]]
-
    '''
+
** [[gungame.core.playersPlayer|Player]]
-
    This callback function can be used for more than 1 attribute, so that you can
+
* [[gungame.core.players.shortcuts]]
-
    register multiple attribute callbacks. All that you have to do is check the
+
** [[gungame.core.playersPlayer|Player]]
-
    name of the attribute (the first argument). The second argument is the value
+
** [[gungame.core.players.shortcuts.deleteAttribute|deleteAttribute(''attribute'')]]
-
    of the attribute. If you raise an error during your callback, the attribute
+
** [[gungame.core.players.shortcuts.addAttributeCallBack|addAttributeCallBack]](''attribute, function, addon'')
-
    will not be set to the attempted value.
+
** [[gungame.core.players.shortcuts.removeAttributeCallBack|removeAttributeCallBack]](''attribute'')
-
    '''
+
** [[gungame.core.players.shortcuts.removeCallBacksForAddon|removeCallBacksForAddon]](''addon'')|}}
-
    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>}}
+

Current revision as of 17:26, 27 April 2009



Function: setAttribute

Module: gungame.core.players.shortcuts
Class: Player
GunGame Version: 5.1

Function Overview

Table of Contents

Contents


Description

A shortcut function for Player.__setattr__() and Player.__setitem__() that allows the scripter to set a custom Player attribute on a single userid or multiple userids using a filter as provided by playerlib.getPlayerList().

Arguments

setAttribute(filter, attribute, value)
  • filter
  • attribute
    • The str() name of the attribute
  • value
    • The value of the attribute


Examples

  • Setting the attribute:
# ../<MOD>/addons/eventscripts/gungame/scripts/custom/gg_sample/gg_sample.py
 
import es
 
from gungame.core.players.shortcuts import setAttribute
from gungame.core.players.shortcuts import deleteAttribute
 
def load():
    # Set the custom attribute "myattribute" on all connected players
    setAttribute('#all', 'myattribute', 0)
 
def unload():
    # Remove the custom attribute "myattribute" from all players
    deleteAttribute('#all', 'myattribute')
 
def gg_start():
    # Set the custom attribute "myattribute" on all connected players
    # (due to the fact that all custom player attributes are removed
    # when the GunGame match ends, we need to re-instantiated the
    # custom attribute "myattribute" when a new GunGame round starts)
    setAttribute('#all', 'myattribute', 0)
 
def player_activate(event_var):
    # Set the custom attribute "myattribute" for this userid
    setAttribute(event_var['userid'], 'myattribute', 0)
 
def player_disconnect(event_var):
    # Delete the custom attribute "myattribute" from this userid
    deleteAttribute(event_var['userid'], 'myattribute')
  • Accessing the attribute:
import es
 
from gungame.core.players.shortcuts import Player
 
def player_say(event_var):
    if event_var['text'] == 'myattribute':
        userid = event_var['userid']
 
        # Using the Player.__getattr__() method
        es.tell(userid, 'Your attribute: %s' %Player(userid).myattribute
 
        # Using the Player.__getitem__() method
        es.tell(userid, 'Your attribute: %s' %Player(userid)['myattribute']

Notes

  • In order to effectively set a custom attribute on all players, use the setAttribute() function in your addon's load(), player_activate(), and gg_start() functions:
def load():
    setAttribute('#all', 'yourattributename', 0)
 
def player_activate(event_var):
    setAttribute(event_var['userid'], 'yourattributename', 0)
 
def gg_start():
    setAttribute(event_var['userid'], 'yourattributename', 0)
  • If you set any custom Player attributes, insure that you remove them when the player disconnects (optional) or when your addon is unloaded (required). This can be accomplished via the deleteAttribute() function.
  • Once a custom Player attribute has been set, you can access the attribute via the Player.__getitem__() or Player.__getattr__() methods.
  • When GunGame starts a new GunGame round, all custom Player attributes are deleted, and need to be re-instantiated. If you use the load(), player_activate(), and gg_start() functions to set custom Player attributes in your script, you will not have to worry about the custom attributes being inaccessible.

See Also

Personal tools