Characters lets every player own several characters and play one of them at a time.
A character is a Character record that belongs to the player's user record and stores a
name, gender, physical description, model and skin, along with the health, armor and
reserve ammo its player had when it was last saved. Players create, load and delete their
characters in the main menu, which opens after the intro when they join; until a character
is loaded the player stays hidden and dead and the HUD is not drawn. The Characters
functions create, save, delete, ban and edit characters on the server, and the Player
extensions return a player's characters and the fields of the active one.
A player may have as many characters as the character_limit config allows, and a name
that a player picks has to be unlike the name of any other character. Staff can ban a
character with /charban add: it can then be neither loaded nor deleted until
/charban remove lifts the ban. Players change their own description with CharPhysDesc.
Other plugins build on it through hooks: PlayerCreateCharacter validates the data of a new
character, PostCreateCharacter and SaveCharacterData let them keep their own fields on a
character, OnActiveCharacterSet and PostCharacterLoaded tell them that a character has
been loaded, and AddCharacterCreationMenuStages and AddMainMenuItems add stages to the
character creation screen and buttons to the main menu. GetCharacterLimit changes the
limit for a player, and PlayerCanUseCharacter, PlayerCanSwitchCharacter and
PlayerCanDeleteCharacter can refuse a request to load or delete a character with a
reason. PlayerNeedsCharacter lets a player stay in the world without a character,
GetCharacterOwner gives a new character to another user record than its creator's, and
Characters.activate and Player:clear_active_character let a player play a character of
another record and drop it again, which runs OnActiveCharacterCleared. A plugin that only
needs to remember something about a character can keep it in the generic character data
(Player:set_character_data) instead of adding a column.
Summary
Hooks
Characters.is_name_taken also knows the characters of players who are not connected.Characters.needs_character), retrying on a timer until the panel can be created.Characters.set_banned saves it before it kills the player.Characters.needs_character exempts them.Functions
character_limit config, unless a GetCharacterLimit hook returns another number.Characters.is_name_taken knows it.Characters.network_data.Hooks
Implements hook ActiveRecordReady
Reads the names of all characters from the database, so that Characters.is_name_taken
also knows the characters of players who are not connected.
Implements hook AddCharacterCreationMenuStages
Adds the general stage (gender, name, description and model) to character creation.
Parameters
- panel Panel the character creation menu
Implements hook AddMainMenuItems
Adds the continue, create character, load character and disconnect buttons to the main menu. There is no continue button while the active character is banned, and the create button only shows a notice once the player has as many characters as they may have.
Parameters
- panel Panel the main menu
- _sidebar Panel the main menu sidebar
Implements hook AddTabMenuItems
Adds the main menu entry to the tab menu.
Parameters
- menu Panel the tab menu
Characters:GetDrawPlayerInfo(target, _x, _y, _distance, lines)
client plugins/characters/plugin/cl_hooks.lua:238Implements hook GetDrawPlayerInfo
Adds the physical description to the lines drawn when looking at a player.
Parameters
- target Player
- _x Number
- _y Number
- _distance Number
- lines Map line definitions keyed by ID, modified in place
Implements hook GetLoadingScreenMessage
Holds the loading screen at 75% until the character list has arrived from the server.
Returns
- String message
- Number percentage; nothing once the characters have arrived
Implements hook OnActiveCharacterSet
Spawns the player and applies the character's model, skin, health, armor and ammo, then runs the PostCharacterLoaded hook.
Parameters
- owner Player
- character Character
Characters:OnCharacterChange(owner, _new_char, _old_char)
server plugins/characters/plugin/sv_hooks.lua:161Implements hook OnCharacterChange
Saves the player's current character before they switch to another one.
Parameters
- owner Player
- _new_char Character
- _old_char Character
Implements hook OnIntroPanelRemoved
Opens the main menu after the intro if the local player has no character and needs one
(see Characters.needs_character), retrying on a timer until the panel can be created.
Implements hook OnThemeLoaded
Registers the main menu and character creation panels with the theme, and recreates the main menu if it is currently open.
Parameters
- current_theme ThemeBase
Implements hook PanelCharacterSet
Applies the character's skin to the model shown in a character panel.
Parameters
- panel Panel the character panel
- char_data Map networked character data
Characters:PlayerCanSwitchCharacter(actor, _character, _current)
server plugins/characters/plugin/sv_hooks.lua:67Implements hook PlayerCanSwitchCharacter
Refuses to let a player switch characters while they are dead or ragdolled.
Parameters
- actor Player
- _character Character the character the player wants to load
- _current Character the active character of the player
Returns
- Boolean false and String the reason when the switch is refused, otherwise nil
Implements hook PlayerCreateCharacter
Validates character creation data: the presence of the player's database record, the character limit, the length of the name, the length of the description unless it was generated, gender, model and skin and, unless the name was generated, that no other character has the name. Text that is not valid UTF-8 is rejected.
Parameters
- actor Player
- data Map character creation data
Returns
- Number CHAR_ERR_* code when the data is rejected, otherwise nil
Characters:PlayerDeath(victim, _inflictor, _attacker)
server plugins/characters/plugin/sv_hooks.lua:56Saves the character of a player who died, unless the character is banned: a banned
character has nothing of its player to keep, and Characters.set_banned saves it before
it kills the player.
Parameters
- victim Player
- _inflictor Entity
- _attacker Entity
Implements hook PlayerDeathThink
Keeps a player whose active character is banned from respawning.
Parameters
- actor Player
Returns
- Boolean false while the active character is banned, otherwise nil
Implements hook PlayerDisconnected
Saves the disconnecting player's character and stops any further saving of their data.
Parameters
- actor Player
Implements hook PlayerInitialized
Opens the intro panel if the local player has no active character.
Implements hook PlayerOneMinute
Saves the player's character; called once a minute for every player.
Parameters
- actor Player
Implements hook PlayerRestored
Sends the character list to the player as soon as they have initialized, then runs the PostRestoreCharacters hook.
Parameters
- actor Player
Implements hook PostCharacterLoaded
On the server
Forwards the PostCharacterLoaded hook to the player's client, passing the character ID.
Parameters
- owner Player
- character Character
On the client
Closes the main menu after a character has been loaded.
Parameters
- _char_id Number
Defined in
Characters:PostCreateCharacter(_owner, char, _char_data)
server plugins/characters/plugin/sv_hooks.lua:125Implements hook PostCreateCharacter
Replaces line breaks in the new character's physical description with ' | '.
Parameters
- _owner Player
- char Character the character being created
- _char_data Map character creation data
Implements hook PostPlayerSpawn
Hides, locks and silently kills players who spawn without an active character or with a
banned one, unless Characters.needs_character exempts them.
Parameters
- actor Player
Characters:RebuildScoreboardPlayerCard(card, target)
client plugins/characters/plugin/cl_hooks.lua:172Implements hook RebuildScoreboardPlayerCard
Adds the Steam name to a scoreboard player card and, unless an IsCharacterCardVisible hook returns false, the player's model icon and physical description.
Parameters
- card Panel the scoreboard player card
- target Player
Implements hook RegisterConditions
Registers the 'character' condition, which compares a player's active character ID.
Implements hook SaveCharacterData
Writes the health, armor and reserve ammo of the player into their active character before it is saved. Nothing is written for a banned character, whose player stays dead, or before the saved values have been given to the player.
Parameters
- owner Player
- character Character the character that is being saved
Implements hook SaveData
Saves the active character of every connected player.
Implements hook ShouldDrawLoadingScreen
Keeps the loading screen visible until the intro panel has been created.
Returns
- Boolean true while there is no intro panel, otherwise nil
Implements hook ShouldHUDPaint
Hides the HUD while the local player has no active character and needs one.
Returns
- Boolean false when no character is loaded and one is needed, otherwise nil
Implements hook ShouldMapsceneRender
Makes the mapscene render while the intro panel or main menu is open.
Returns
- Boolean true while the panel is valid, otherwise nil
Implements hook ShouldScoreboardHide
Lets the scoreboard hide only when the local player has an active character or does not need one.
Returns
- Boolean
Implements hook ShouldScoreboardShow
Lets the scoreboard show only when the local player has an active character or does not need one.
Returns
- Boolean
Mutes the menu music while the game window is unfocused and fades it out and stops it once the intro panel is gone.
Functions
Makes a character the active character of a player. Runs OnCharacterChange if another
character was active, networks the basic character data and runs OnActiveCharacterSet. The
character does not have to belong to the player: one of another user record is sent to the
player on its own, so that their client knows the character it is playing. Nothing is
checked here: the requests of players go through Characters.can_use first, which is what
keeps banned characters from being loaded.
Parameters
- target Player
- character Character any character record; nothing happens when it is not a table
Returns the networkable data of every character of a player. Server only.
Parameters
- target Player
Returns
- List<Map> one entry per character, empty when the player has no record
See also
Checks whether a player may delete one of their characters. The active character and banned characters are always refused; anything else has to pass the PlayerCanDeleteCharacter hook. Server only.
Parameters
- actor Player
- character Character the character to delete; refused when nil
Returns
- Boolean true when the character may be deleted; otherwise false
- String the reason as a language phrase and Map the arguments of the phrase
Checks whether a player may load one of their characters. A banned character and the character that is already active are always refused. A player who is switching away from a character that is not banned has to pass the PlayerCanSwitchCharacter hook, and every load the PlayerCanUseCharacter hook. Server only.
Parameters
- actor Player
- character Character the character to load; refused when nil
Returns
- Boolean true when the character may be loaded; otherwise false
- String the reason as a language phrase and Map the arguments of the phrase
Changes the physical description of a player's active character on their own request.
Control characters other than line breaks become spaces, the text is trimmed, its line
breaks are replaced with ' | ', and it is refused when its length is outside of the
character_min_desc_len and character_max_desc_len configs, when the player has no
character or when they changed it less than a second ago. The player is notified of the
outcome. Server only.
Parameters
- actor Player
- text String the new description
Returns
- Boolean true when the description was changed
Creates a character for a player unless a PlayerCreateCharacter hook rejects the data. On the server the character is then saved and sent to its owner.
local status = Characters.create(target, {
name = 'John Doe',
phys_desc = 'A tall man in a worn coat.',
gender = CHAR_GENDER_MALE,
model = 'models/humans/group01/male_02.mdl',
skin = 0
})
if status != CHAR_SUCCESS then
-- status is one of the CHAR_ERR_* codes
end
The new character belongs to the user record of the player unless a GetCharacterOwner hook
hands it to another User record; only for a character of the player's own is their
character list sent to them again.
Parameters
- target Player the player the character is created for, its owner unless a GetCharacterOwner hook names another record
- data Map creation data: name, phys_desc, gender, model and optionally skin. Set name_generated to true when the name was not chosen by the player, which lets it repeat the name of another character, and description_generated to true when the description was not written by the player, which frees it from the length limits
Returns
- Number CHAR_SUCCESS, or the CHAR_ERR_* code returned by the hook
Destroys one of the player's characters, removes it from their record, frees its name
and resends the character list to the player. Nothing is checked here: the requests of
players go through Characters.can_delete first. Server only.
Parameters
- target Player
- id Number character ID
Finds the characters that have a name, compared without regard to letter case. The characters of the user records that are loaded are searched first: those of connected players and those that a CollectLoadedUserRecords handler adds. Only when none of them matches are the characters of offline players loaded from the database, which makes the callback run later. Server only.
Parameters
- name String
- callback Function receives a List<Character>, empty when nothing was found
Returns
- Any what the callback returns if a loaded character matches, nil otherwise
Records that a character no longer bears a name. Characters.delete and
Characters.set_name call it themselves. Server only.
Parameters
- name String
Characters.get_custom_data(character, key, default)
shared plugins/characters/plugin/lib/sh_character.lua:77Returns a value from the generic data of a character.
On the client only the keys registered with Characters.network_data are known, and only
for the characters of the local player.
Parameters
- character Character/Map character record, or networked character data on the client
- key String
- default Any optional, defaults to
nilvalue to return when nothing is stored under the key
Returns
- Any
Returns how many characters a player may have: the character_limit config, unless a
GetCharacterLimit hook returns another number.
Parameters
- target Player
Returns
- Number
Returns the player a character belongs to, if they are on the server. A character whose user record has no connected player belongs to the connected player who has it as their active character, if there is one. Server only.
Parameters
- character Character
Returns
- Player the owner, or nil when nobody on the server has the character
Checks whether a key of the generic character data is sent to the owner of the character.
Parameters
- key String key of the character data
Returns
- Boolean
Checks whether a character of any player, connected or not, already has a name. Names are compared without regard to letter case and surrounding or repeated whitespace. The names are read from the database when the server starts. Server only.
Parameters
- name String
- except Character optional, defaults to
nilcharacter whose own name does not count, for renaming it
Returns
- Boolean
Checks whether a player has as many characters as they may have. On the server the player must have their database record, on the client only the local player can be checked.
Parameters
- target Player
Returns
- Boolean true when the player cannot create another character
See also
Checks whether a player has to have an active character to be in the world. A player who does is kept hidden and dead, without a HUD or a scoreboard, until they load one, and the main menu opens for them after the intro. Plugins exempt a player through the PlayerNeedsCharacter hook, which is how gamemasters stay in the world without a character.
Parameters
- target Player
Returns
- Boolean false when a PlayerNeedsCharacter handler has returned false, otherwise true
Makes the server send a key of the generic character data to the owner of the character.
Without it the data never leaves the server. Once a key is registered, its value is part of
the character list that the owner receives and is sent again whenever it is set, so the
owner can read it with Player:get_character_data; other players never receive it. Call
it while the plugin that owns the key loads. It only has an effect on the server, but may
be called on both realms.
Characters.network_data('flags')
Parameters
- key String key of the character data
Records that a character bears a name, so that Characters.is_name_taken knows it.
Characters.create and Characters.set_name call it themselves. Server only.
Parameters
- name String
Characters.restore_vitals(owner, character)
server plugins/characters/plugin/lib/sh_character.lua:540Gives a player the health, armor and reserve ammo saved with a character. Without saved health the player keeps full health. Server only.
Parameters
- owner Player
- character Character
Runs the SaveCharacterData hook and saves the player's record to the database, unless a PreSaveCharacter hook returns false. A character that belongs to another user record than the player's is saved on its own, together with its own relations. Server only.
Parameters
- target Player the player whose data goes into the character
- character Character
Sends the networkable data of all of a player's characters to that player. Server only.
Parameters
- target Player
Bans or unbans a character and saves it. A banned character cannot be loaded or deleted by its owner and is marked as banned in their character list. If the character is the active one of a connected player, its health, armor and ammo are saved first, the player is killed silently and kept from respawning, and the main menu opens for them so that they can pick another character. Unbanning the active character of a connected player respawns them with what was saved. Runs the CharacterBanChanged hook. Server only.
Parameters
- character Character a record of a connected or an offline player
- banned Boolean
Characters.set_custom_data(character, key, value)
server plugins/characters/plugin/lib/sh_character.lua:289Sets a value in the generic data of a character. The value is written to the database
the next time the character is saved; values must be serializable, so no functions. If
the key is registered with Characters.network_data and the owner of the character is
connected, the new value is sent to them. A table that is changed in place has to be set
again to be sent. Server only.
Characters.set_custom_data(character, 'spawn_position', target:GetPos())
Parameters
- character Character
- key String nothing happens when it is not a string
- value Any nil removes the key
Changes the physical description of the player's active character, networks it and runs the CharacterDescChanged hook. Server only.
Parameters
- target Player
- new_desc String ignored when it is not a string
Changes the gender of the player's active character, networks it and runs the CharacterGenderChanged hook. Server only.
Parameters
- target Player
- new_gender Number/String CHAR_GENDER_* value, or 'male', 'female' or 'no_gender'
Changes the model of the player and of their active character, networks it and runs the CharacterModelChanged hook. Server only.
Parameters
- target Player
- model String model path; ignored when it is not a string
Changes the name of the player's active character, networks it and runs the
CharacterNameChanged hook. The name is not checked against the names of other
characters; use Characters.is_name_taken first where that matters. Server only.
Parameters
- target Player
- new_name String ignored when it is not a string
Writes the health, armor and reserve ammo of a player into a character. A player who is dead leaves the character without saved health, armor and ammo, so that it is loaded with full health next time. Server only.
Parameters
- owner Player
- character Character
Builds the table of character fields that is sent to the owning client: id, user_id,
steam_id, name, gender, phys_desc, model, skin, banned and custom_data, which holds the
keys of the generic data registered with Characters.network_data. Server only.
Parameters
- target Player owner of the character
- char Character
Returns
- Map character data, or nil if the player or the character is not valid