Player class
Part of Characters System
Player extensions of the Characters plugin: the characters of a player and the active one.
A player owns a list of characters and has at most one active character at a time. The
player methods return these characters (Player:get_all_characters, Player:get_character)
and the fields of the active one; on the server they are Character records, on the client
the networked tables that the server sends to the owner of the characters. Server-side
methods select the active character, set its fields and save it.
This file also holds the Characters functions that create, save, delete, ban and edit
characters. A request of a player to load or delete a character is checked first:
Characters.can_use and Characters.can_delete apply the rules of the plugin and run the PlayerCanUseCharacter,
PlayerCanSwitchCharacter and PlayerCanDeleteCharacter hooks, and a refused request is
answered with a reason that the menu shows.
Every character also has generic data: a table of values kept under string keys, saved with
the character in its data column and read and written with Player:get_character_data
and Player:set_character_data (or Character:get_data and Character:set_data). The
data stays on the server unless a key is passed to Characters.network_data, which makes
the server send the value of that key to the owner of the character, and to nobody else.
Summary
Methods
Methods
Leaves the player without an active character. The active character is saved first and the OnActiveCharacterCleared hook is run while it is still active; then the player's name goes back to their Steam name and their gender, description and model to the defaults. The player is neither spawned nor killed here.
Returns all characters that belong to the player. Clients only know their own characters.
Returns
- List character records on the server, networked character data on the client
Returns the player's active character. Bots get a plain table that is created on demand.
Returns
- Character/Map the character record on the server or its networked data on the client, nil if no character is active
Finds one of the player's own characters by its ID. On the client the active character of the local player is found as well when it belongs to somebody else, such as a character that the player is playing for its owner.
Parameters
- id Number character ID
Returns
- Character/Map the character record on the server or its networked data on the client, nil if the player has no such character
Returns a value from the generic data of the player's active character. The server
knows all of it; a client only knows the keys registered with
Characters.network_data, and only for the local player.
local position = target:get_character_data('spawn_position')
Parameters
- key String
- default Any optional, defaults to
nilvalue to return when nothing is stored under the key or no character is active
Returns
- Any
See also
Returns the networked ID of the player's active character.
Returns
- Number character ID, or nil if no character has been selected
Returns a field of the player's active character. On the client the value is read from the player's networked variable of the same name instead.
Parameters
- id String field name
- default Any optional, defaults to
nilvalue to return when the field is not set
Returns
- Any
Returns the gender of the player's character as a string.
Returns
- String 'male', 'female' or 'no_gender'
Returns the physical description of the player's character.
Returns
- String the description, or a placeholder text if none is set
Checks whether the player's active character is banned. A player whose active character gets banned stays dead until they load another one. On the client only the local player can be checked.
Returns
- Boolean false when the player has no active character
Checks whether the player has an active character. Always true for bots.
Returns
- Boolean true if a character is active, false or nil otherwise
Saves the player's active character, if they have one.
See also
Makes one of the player's own characters their active one.
Parameters
- id Number/String character ID; nothing happens if the player has no such character
See also
Sets a value in the generic data of the player's active character. Unlike
Player:set_character_var, the value needs no column of its own and is not networked to
every player: it is saved with the character the next time the character is saved, and
sent to the player themselves only if the key is registered with
Characters.network_data.
target:set_character_data('spawn_position', target:GetPos())
Parameters
- key String nothing happens when it is not a string
- value Any anything serializable, so no functions; nil removes the key
See also
Sets a field on the player's active character and networks it under the same name.
Parameters
- id String field name; nothing happens when it is not a string
- val Any