Skip to content

API: Statistics

Robert Fraser edited this page Aug 27, 2019 · 1 revision

Statistics

Statistics are used as a fun way to track what players did over the course of the gamemode. At the end of a game, these are displayed in a highscore table and experience points are awarded.

Statistics have a string key, such as 'Rounds Played', and a number of points associated with them.

GM:AddStatConversion(type, nicename, value, max)

Adds a conversion for a given statistics key to amount of XP. This should be called in the RegisterStatsConversions hook, eg:

hook.Add('RegisterStatsConversions', 'AddBombTagStatConversions', function()
    GAMEMODE:AddStatConversion('Bomb Passes', 'Bomb Tagged', 0.5)
    GAMEMODE:AddStatConversion('Explosions Survived', 'Explosions Survived', 0.25)
end)

By default the maximum amount of XP per category is 25XP. This can be adjusted if needed.

GM:GetStatWinner(statistic)

Get the winning player for a given statistic. This is the player with the highest number of points in the given statistic.

GM:GenerateStatisticsTable()

Generates an overall statistics table. The keys of this table are the statistic names.

Player:GetStat(statistic)

Get the current number of points a given player has in the statistic.

Player:GetStatTable()

Generate a table tracking all the player statistics. The keys of this table are the statistic names.

Player:AddStatPoints(statistic, amount)

Adds a given number of points to a given players statistic.

v:AddStatPoints('Survived Rounds', 1)
v:AddStatPoints('Cubes Collected', 5)

Player:LoadStatsFromDB()

Internal function for loading statistics. Should not be used in gamemodes.

Player:UpdateStatsFromDB()

Internal function for saving statistics. Should not be used in gamemodes.