Gungame.core.players.shortcuts.setAttribute
From GunGame5 Documentation
Revision as of 17:22, 27 April 2009 by GunGame5WikiAdmin (Talk | contribs)
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
- userid
- A str() filter as used by playerlib.getPlayerList()
- 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(), [[1]], 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.
- Other scripts can also access your custom attributes 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(), [http://www.eventscripts.com/pages/Player_activate
See Also
- gungame.core.players
- gungame.core.players.shortcuts
- Player
- deleteAttribute(attribute)
- addAttributeCallBack(attribute, function, addon)
- removeAttributeCallBack(attribute)
- removeCallBacksForAddon(addon)
- Forum search for Gungame.core.players.shortcuts.setAttribute