File tree Expand file tree Collapse file tree 3 files changed +42
-2
lines changed
docs/source-python/source
packages/source-python/players Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,36 @@ Your plugin should now look like this.
124124 player.health += EXTRA_HP
125125
126126
127+ Alternatively, you can use the classmethod
128+ :meth: `players.entity.Player.from_userid `. It's a wrapper around
129+ :func: `players.helpers.index_from_userid ` and will shorten your code in
130+ events.
131+
132+ .. code-block :: python
133+
134+ from events import Event
135+ from players.entity import Player
136+ from players.helpers import index_from_userid
137+ from messages import SayText2
138+
139+ # Extra amount of health every player should get on spawn
140+ EXTRA_HP = 25
141+
142+ def load ():
143+ SayText2(' Plugin has been loaded successfully!' ).send()
144+
145+ def unload ():
146+ SayText2(' Plugin has been unloaded successfully!' ).send()
147+
148+ @Event (' player_spawn' )
149+ def on_player_spawn (game_event ):
150+ # Create a Player object from the user ID...
151+ player = Player.from_userid(game_event[' userid' ])
152+
153+ # ... and add some extra HP
154+ player.health += EXTRA_HP
155+
156+
127157 What's next?
128158------------
129159
Original file line number Diff line number Diff line change @@ -5,8 +5,8 @@ Welcome to the Source.Python documentation!
55
66Source.Python is an open-source project that uses Boost.Python to allow
77scripters to interact with Valve's Source-engine. In this wiki, you will find
8- documentation on the how to install, update, and use Source.Python. If you
9- have any questions, please search for an answer on our
8+ documentation on how to install, update, and use Source.Python. If you have
9+ any questions, please search for an answer on our
1010`forums <http://www.sourcepython.com/forum.php >`_. If you cannot find the
1111answer to your question, please post a question of your own in the appropriate
1212forum.
Original file line number Diff line number Diff line change 4141from players .helpers import address_from_playerinfo
4242from players .helpers import get_client_language
4343from players .helpers import playerinfo_from_index
44+ from players .helpers import index_from_userid
4445from players .helpers import uniqueid_from_playerinfo
4546from players .games import _GamePlayer
4647from players .voice import mute_manager
@@ -71,6 +72,15 @@ def __init__(self, index):
7172 super ().__init__ (index )
7273 object .__setattr__ (self , '_playerinfo' , None )
7374
75+ @classmethod
76+ def from_userid (cls , userid ):
77+ """Create an instance from a userid.
78+
79+ :param int userid: The userid.
80+ :rtype: Player
81+ """
82+ return cls (index_from_userid (userid ))
83+
7484 @property
7585 def permissions (self ):
7686 """Return the player's :class:`auth.manager.PlayerPermissions` object."""
You can’t perform that action at this time.
0 commit comments