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

Leaves the player without an active character.
Returns all characters that belong to the player.
Returns the player's active character.
Finds one of the player's own characters by its ID.
Returns a value from the generic data of the player's active character.
Returns the networked ID of the player's active character.
Returns a field of the player's active character.
Returns the gender of the player's character as a string.
Returns the physical description of the player's character.
Checks whether the player's active character is banned.
Checks whether the player has an active character.
Saves the player's active character, if they have one.
Makes one of the player's own characters their active one.
Sets a value in the generic data of the player's active character.
Sets a field on the player's active character and networks it under the same name.

Methods

Player:clear_active_character()

server plugins/characters/plugin/sv_plugin.lua:71

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.

Player:get_all_characters()

shared plugins/characters/plugin/lib/sh_character.lua:980

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

Player:get_character_by_id(id)

shared plugins/characters/plugin/lib/sh_character.lua:875

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

Player:get_character_data(key, default)

shared plugins/characters/plugin/lib/sh_character.lua:948

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 nil value to return when nothing is stored under the key or no character is active

Returns

  • Any

See also

Player:get_character_id()

shared plugins/characters/plugin/lib/sh_character.lua:895

Returns the networked ID of the player's active character.

Returns

  • Number character ID, or nil if no character has been selected

Player:get_character_var(id, default)

shared plugins/characters/plugin/lib/sh_character.lua:929

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 nil value 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

Player:is_character_banned()

shared plugins/characters/plugin/lib/sh_character.lua:958

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

Player:is_character_loaded()

shared plugins/characters/plugin/lib/sh_character.lua:916

Checks whether the player has an active character. Always true for bots.

Returns

  • Boolean true if a character is active, false or nil otherwise

Player:save_character()

server plugins/characters/plugin/sv_plugin.lua:132

Saves the player's active character, if they have one.

See also

Player:set_active_character(id)

server plugins/characters/plugin/sv_plugin.lua:55

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

Player:set_character_data(key, value)

server plugins/characters/plugin/sv_plugin.lua:120

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

Player:set_character_var(id, val)

server plugins/characters/plugin/sv_plugin.lua:102

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

Defined in