Gungame.core.players.shortcuts.addAttributeCallBack

From GunGame5 Documentation

(Difference between revisions)
Jump to: navigation, search
 
(One intermediate revision not shown)
Line 4: Line 4:
class=[[gungame.core.players.Player|Player]]|
class=[[gungame.core.players.Player|Player]]|
ggversion=5.1|
ggversion=5.1|
-
description=A shortcut function for the [[:Category:Static Method|static class method]] [[gungame.core.players.Player.addAttributeCallBack|Player.addAttributeCallBack()]] which adds a callback to be triggered when the declared attribute is set via [[gungame.core.players.Player|Player.__setitem__]] or [[gungame.core.players.Player|Player.__setattr__]] via the [[gungame.core.players.Player|Player()]] class. If the attribute does not exist at the time of the callback declaration, no errors are raised, and the callback is created irregardless. Two arguments must be provided for the callback function. The first will return the name of the attribute, and the second will return the value that is attempting to be set for the attribute. If you raise an error during your callback, the attribute will not be set.|
+
description=A shortcut function for the [[:Category:Static Method|static class method]] [[gungame.core.players.Player.addAttributeCallBack|Player.addAttributeCallBack()]] which adds a callback to be triggered when the declared attribute is set via [[gungame.core.players.Player|Player.__setitem__]] or [[gungame.core.players.Player|Player.__setattr__]] via the [[gungame.core.players.Player|Player()]] class.|
arguments=<source lang="python">addAttributeCallback(attribute, function, addon)</source>
arguments=<source lang="python">addAttributeCallback(attribute, function, addon)</source>
* '''attribute'''
* '''attribute'''
Line 55: Line 55:
             pass
             pass
         else:
         else:
-
             raise ValueError('Value must be between 0 and 10!')</source>}}
+
             raise ValueError('Value must be between 0 and 10!')</source>|
 +
 
 +
notes=* If the attribute does not exist at the time of the callback declaration, no errors are raised, and the callback is created irregardless.
 +
* Two arguments must be provided for the callback function. The first will return the name of the attribute, and the second will return the value that is attempting to be set for the attribute.
 +
* If you raise an error during the callback, the attribute will not be set.|}}

Current revision as of 15:43, 27 April 2009



Function: addAttributeCallback

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

Function Overview

Table of Contents

Contents


Description

A shortcut function for the static class method Player.addAttributeCallBack() which adds a callback to be triggered when the declared attribute is set via Player.__setitem__ or Player.__setattr__ via the Player() class.

Arguments

addAttributeCallback(attribute, function, addon)
  • attribute
    • A str() attribute name.
  • function
    • A custom function.
  • addon
    • The name of the GunGame addon calling this function.


Examples

# ../<MOD>/addons/eventscripts/gungame/scripts/custom/gg_sample/gg_sample.py
 
from gungame.core.players.shortcuts import setAttribute
from gungame.core.players.shortcuts import addAttributeCallBack
from gungame.core.players.shortcuts import removeAttributeCallBack
from gungame.core.players.shortcuts import removeCallBacksForAddon
 
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)
    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
    removeAttributeCallBack('myattribute')
 
    # Remove all custom attribute callbacks for this addon
    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!')

Notes

  • If the attribute does not exist at the time of the callback declaration, no errors are raised, and the callback is created irregardless.
  • Two arguments must be provided for the callback function. The first will return the name of the attribute, and the second will return the value that is attempting to be set for the attribute.
  • If you raise an error during the callback, the attribute will not be set.

See Also

{{{seealso}}}

Personal tools