ISteamUserStats¶
List of Functions¶
List of Callbacks¶
Function Reference¶
- userStats.getStatInt(name)¶
- Parameters:
name (string) – The ‘API Name’ of the stat. Must not be longer than 128 bytes.
- Returns:
(boolean) This function returns true upon success if all of the following conditions are met; otherwise, false.
userStats.requestCurrentStats()
has completed and successfully returned its callback.The ‘API Name’ of the specified stat exists in App Admin on the Steamworks website, and the changes are published.
- Returns:
(number?) If the call is successful, returns a second value, the current value of the stat.
- SteamWorks:
Gets the current value of a users stat, it must have been set as an int. For floats see
userStats.getStatFloat()
The equivalent function for other users is
userStats.getUserStatInt()
(missing).
Example:
local success, data = Steam.userStats.getStatInt('stat_name')
if success then
print(data)
end
- userStats.getStatFloat(name)¶
Same as
userStats.getStatInt()
, but this function reads stats that are set as floats.The equivalent function for other users is
userStats.getUserStatFloat()
(missing).
Example:
local success, data = Steam.userStats.getStatFloat('stat_name')
if success then
print(data)
end
- userStats.setStatInt(stat_name, stat_value)¶
- Parameters:
name (string) – The ‘API Name’ of the stat whose value to set. Must not be longer than 128 bytes.
stat_value (int) – The new value of the stat. This must be an absolute value, it will not increment or decrement for you.
- Returns:
(boolean) This function returns true upon success if all of the following conditions are met; otherwise, false.
The specified stat ‘API Name’ exists in App Admin on the Steamworks website, and the changes are published.
userStats.requestCurrentStats()
has completed and successfully returned its callback.
- SteamWorks:
Sets the value of a stat for the current user.
You must have called
userStats.requestCurrentStats()
and it needs to return successfully via its callback prior to calling this!This call only modifies Steam’s in-memory state so it is quite cheap. To submit the stats to the server you must call
userStats.storeStats()
.
Example:
Steam.userStats.setStatInt('stat_name', 43)
Steam.userStats.storeStats()
- userStats.setStatFloat(stat_name, stat_value)¶
Same as
userStats.setStatInt()
, but this function sets stats as floats.
Example:
Steam.userStats.setStatFloat('stat_name', 43.34)
Steam.userStats.storeStats()
- userStats.getAchievement(name)¶
- Parameters:
name (string) – The ‘API Name’ of the achievement.
- Returns:
(boolean) This function returns true upon success if all of the following conditions are met; otherwise, false.
userStats.requestCurrentStats()
has completed and successfully returned its callback.The ‘API Name’ of the specified achievement exists in App Admin on the Steamworks website, and the changes are published.
- Returns:
(boolean?) If the call is successful, returns a second value, the unlock status of the achievement.
- SteamWorks:
Gets the unlock status of the Achievement.
The equivalent function for other users is
userStats.getUserAchievement()
(missing).
Example:
local success, achieved = Steam.userStats.getAchievement('ach_name')
if success and achieved then
print('Yep, you have the achievement')
end
- userStats.setAchievement(name)¶
- Parameters:
name (string) – The ‘API Name’ of the Achievement to unlock.
- Returns:
(boolean) This function returns true upon success if all of the following conditions are met; otherwise, false.
The specified achievement ‘API Name’ exists in App Admin on the Steamworks website, and the changes are published.
userStats.requestCurrentStats()
has completed and successfully returned its callback.
- SteamWorks:
Unlocks an achievement.
You must have called
userStats.requestCurrentStats()
and it needs to return successfully via its callback prior to calling this!You can unlock an achievement multiple times so you don’t need to worry about only setting achievements that aren’t already set. This call only modifies Steam’s in-memory state so it is quite cheap. To send the unlock status to the server and to trigger the Steam overlay notification you must call
userStats.storeStats()
.
Example:
if achievementConditionSatisfied() and doesntHaveAchievement() then
Steam.userStats.setAchievement('ach_name')
Steam.userStats.storeStats() -- shows overlay notification
end
- userStats.resetAllStats(achievementsToo)¶
- Parameters:
achievementsToo (boolean) – Also reset the user’s achievements?
- Returns:
(boolean) true indicating success if
userStats.requestCurrentStats()
has already been called and successfully returned its callback; otherwise false.- SteamWorks:
Resets the current users stats and, optionally achievements.
This automatically calls
userStats.storeStats()
to persist the changes to the server. This should typically only be used for testing purposes during development. Ensure that you sync up your stats with the new default values provided by Steam after calling this by callinguserStats.requestCurrentStats()
.
Example:
if dev_mode and keypressed('f10') then
Steam.userStats.resetAllStats(true)
end
- userStats.storeStats()¶
- Returns:
(boolen) This function returns true upon success if all of the following conditions are met; otherwise, false.
userStats.requestCurrentStats()
has completed and successfully returned its callback.The current game has stats associated with it in the Steamworks Partner backend, and those stats are published.
- SteamWorks:
Send the changed stats and achievements data to the server for permanent storage.
If this fails then nothing is sent to the server. It’s advisable to keep trying until the call is successful.
This call can be rate limited. Call frequency should be on the order of minutes, rather than seconds. You should only be calling this during major state changes such as the end of a round, the map changing, or the user leaving a server. This call is required to display the achievement unlock notification dialog though, so if you have called
userStats.setAchievement()
then it’s advisable to call this soon after that.If you have stats or achievements that you have saved locally but haven’t uploaded with this function when your application process ends then this function will automatically be called.
You can find additional debug information written to the
%steam_install%\logs\stats_log.txt
file.If the call is successful you will receive a
userStats.userStatsStored()
callback. If result has a result of “InvalidParam”, then one or more stats uploaded has been rejected, either because they broke constraints or were out of date. In this case the server sends back updated values and the stats should be updated locally to keep in sync.If one or more achievements has been unlocked then this will also trigger a
userStats.userAchievementStored()
callback.
Example:
function onMatchEnd()
Steam.userStats.storeStats()
end
- userStats.requestCurrentStats()¶
- Returns:
(boolean) Only returns false if there is no user logged in; otherwise, true.
- SteamWorks:
Asynchronously request the user’s current stats and achievements from the server.
You must always call this first to get the initial status of stats and achievements. Only after the resulting callback comes back can you start calling the rest of the stats and achievement functions for the current user.
The equivalent function for other users is
userStats.requestUserStats()
(missing).Triggers a
userStats.onUserStatsReceived()
callback.
Example:
-- before any achievement/stats stuff
Steam.userStats.requestCurrentStats()
function Steam.userStats.onUserStatsReceived()
can_do_stats_stuff = true
end
- userStats.findLeaderboard(name, callback)¶
- Parameters:
name (string) – The name of the leaderboard to find. Must not be longer than 128 bytes.
callback (function) – Called asynchronously when this function returns. See below.
- Returns:
nothing
- SteamWorks:
Gets a leaderboard by name.
You must call either this or
userStats.findOrCreateLeaderboard()
to obtain the leaderboard handle which is valid for the game session for each leaderboard you wish to access prior to calling any other Leaderboard functions.callback(data, err) receives two arguments:
data (table) – Similar to LeaderboardFindResult_t, or nil if err is true.
data.steamLeaderboard (uint64) – Handle to the leaderboard that was searched for. A special value is returned if no leaderboard was found.
data.leaderboardFound (boolean) – Was the leaderboard found? true if it was, false if it wasn’t.
err (boolean): true if there was any IO error with the request.
Example:
Steam.userStats.findLeaderboard('l_name', function(data, err)
if err or not data.leaderboardFound then
print('Something happened')
elseif
uploadScoresHelper(data.steamLeaderboard)
end
end)
- userStats.findOrCreateLeaderboard(name, sortMethod, displayType, callback)¶
- Parameters:
name (string) – The name of the leaderboard to find or create. Must not be larger than 128 bytes.
sortMethod (string) – The sort order of the new leaderboard if it’s created. Must be ‘Ascending’ or ‘Descending’ (see ELeaderboardSortMethod).
displayType (string) – The display type (used by the Steam Community web site) of the new leaderboard if it’s created. Must be one of: ‘Numeric’, ‘TimeSeconds’ or ‘TimeMilliSeconds’ (see ELeaderboardDisplayType).
callback (function) – Called asynchronously when this function returns. It must be of the same type as the callback in
userStats.findLeaderboard()
.
- Returns:
nothing
- SteamWorks:
Gets a leaderboard by name, it will create it if it’s not yet created.
You must call either this or
userStats.findLeaderboard()
to obtain the leaderboard handle which is valid for the game session for each leaderboard you wish to access prior to calling any other Leaderboard functions.Leaderboards created with this function will not automatically show up in the Steam Community. You must manually set the Community Name field in the App Admin panel of the Steamworks website. As such it’s generally recommended to prefer creating the leaderboards in the App Admin panel on the Steamworks website and using
userStats.findLeaderboard()
unless you’re expected to have a large amount of dynamically created leaderboards.
Example:
Steam.userStats.findOrCreateLeaderboard('l_name', 'Ascending', 'Numeric', function(data, err)
if err or not data.leaderboardFound then
print('Something happened')
elseif
uploadScoresHelper(data.steamLeaderboard)
end
end)
- userStats.getLeaderboardName(steamLeaderboard)¶
- Parameters:
steamLeaderboard (uint64) – A leaderboard handle obtained from
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
.- Returns:
(string) The name of the leaderboard. Returns an empty string if the leaderboard handle is invalid.
- SteamWorks:
Returns the name of a leaderboard handle.
Example:
function printLeaderboardInfo(handle)
print('Leaderboard name: ' .. Steam.userStats.getLeaderboardName(handle))
print('Entries: ' .. Steam.userStats.getLeaderboardEntryCount(handle))
print('Sort Method: ' .. Steam.userStats.getLeaderboardSortMethod(handle))
print('Display Type: ' .. Steam.userStats.getLeaderboardDisplayType(handle))
end
- userStats.getLeaderboardEntryCount(steamLeaderboard)¶
- Parameters:
steamLeaderboard (uint64) – A leaderboard handle obtained from
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
.- Returns:
(number) The number of entries in the leaderboard. Returns 0 if the leaderboard handle is invalid.
- SteamWorks:
Returns the total number of entries in a leaderboard.
This is cached on a per leaderboard basis upon the first call to
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
and is refreshed on each successful call touserStats.downloadLeaderboardEntries()
,userStats.downloadLeaderboardEntriesForUsers()
(missing), anduserStats.uploadLeaderboardScore()
.
Example: See userStats.getLeaderboardName()
’s example.
- userStats.getLeaderboardSortMethod(steamLeaderboard)¶
- Parameters:
steamLeaderboard (uint64) – A leaderboard handle obtained from
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
.- Returns:
(string?) The sort method of the leaderboard, either “Ascending” or “Descending”. Returns nil if the leaderboard handle is invalid.
- SteamWorks:
Returns the sort order of a leaderboard handle.
Example: See userStats.getLeaderboardName()
’s example.
- userStats.getLeaderboardDisplayType(steamLeaderboard)¶
- Parameters:
steamLeaderboard (uint64) – A leaderboard handle obtained from
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
.- Returns:
(string?) The display type of the leaderboard, one of “Numeric”, “TimeSeconds” or “TimeMilliSeconds”. Returns nil if the leaderboard handle is invalid.
- SteamWorks:
Returns the display type of a leaderboard handle.
Example: See userStats.getLeaderboardName()
’s example.
- userStats.uploadLeaderboardScore(steamLeaderboard, uploadScoreMethod, score, details, callback)¶
- Parameters:
steamLeaderboard (uint64) – A leaderboard handle obtained from
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
.uploadScoreMethod (string) – Do you want to force the score to change, or keep the previous score if it was better? Either “KeepBest” or “ForceUpdate”.
score (number) – The score to upload. Must be a 32-bit integer.
details (string?) – Optional string with details surrounding the unlocking of this score. Size must be a multiple of four, and at most 256 bytes. Will be converted to an array of 32-bit integers.
callback (function) – Called asynchronously when this function returns. See below.
- Returns:
nothing
- SteamWorks:
Uploads a user score to a specified leaderboard.
Details are optional game-defined information which outlines how the user got that score. For example if it’s a racing style time based leaderboard you could store the timestamps when the player hits each checkpoint. If you have collectibles along the way you could use bit fields as booleans to store the items the player picked up in the playthrough.
Uploading scores to Steam is rate limited to 10 uploads per 10 minutes and you may only have one outstanding call to this function at a time.
callback(data, err) receives two arguments:
data (table) – Similar to LeaderboardScoreUploaded_t, or nil if there was err is true.
data.success (boolean) – Was the call successful? Returns true if the call was successful, false on failure, for example:
The amount of details sent exceeds 256 bytes.
The leaderboard is set to “Trusted” in App Admin on Steamworks website, and will only accept scores sent from the Steam Web API.
data.steamLeaderboard (uint64) – Handle to the leaderboard that was searched for. A special value is returned if no leaderboard was found.
data.score (number) – The score that was attempted to set.
data.scoreChanged (boolean) – true if the score on the leaderboard changed otherwise false if the existing score was better.
data.globalRankNew (number) – The new global rank of the user on this leaderboard.
data.globalRankPrevious (number) – The previous global rank of the user on this leaderboard; 0 if the user had no existing entry in the leaderboard.
err (boolean): true if there was any IO error with the request.
Example:
function uploadScoresHelper(handle)
local score = getScore()
Steam.userStats.uploadLeaderboardScore(handle, "KeepBest", score, nil, function(data, err)
if err or not data.success then
print('Upload score failed')
else
print('Upload score success. New rank is: ' .. data.globalRankNew)
end
end)
end
- userStats.downloadLeaderboardEntries(steamLeaderboard, dataRequest, rangeStart, rangeEnd, callback)¶
- userStats.downloadLeaderboardEntries(steamLeaderboard, dataRequest, callback)
- Parameters:
steamLeaderboard (uint64) – A leaderboard handle obtained from
userStats.findLeaderboard()
oruserStats.findOrCreateLeaderboard()
.dataRequest (string) – The type of data request to make. Must be one of “Global”, “GlobalAroundUser” or “Friends” (see ELeaderboardDataRequest).
rangeStart (number) – The index to start downloading entries relative to dataRequest. Must not be supplied if dataRequest is “Friends”.
rangeEnd (number) – The last index to retrieve entries relative to dataRequest. Must not be supplied if dataRequest is “Friends”.
callback (function) – Called asynchronously when this function returns. See below.
- Returns:
nothing
- SteamWorks:
Fetches a series of leaderboard entries for a specified leaderboard.
You can ask for more entries than exist, then this will return as many as do exist.
If you want to download entries for an arbitrary set of users, such as all of the users on a server then you can use
userStats.downloadLeaderboardEntriesForUsers()
(missing) which takes an array of Steam IDs.callback(data, err) receives two arguments:
data (table) – An array of tables similar to LeaderboardEntry_t, or nil if there was err is true.
data[i].steamIDUser (uint64) – User who this entry belongs to. You can use
friends.getFriendPersonaName()
andfriends.getSmallFriendAvatar()
(missing) to get more info.data[i].globalRank (number) – The global rank of this entry ranging from [1..N], where N is the number of users with an entry in the leaderboard.
data[i].score (number) – The raw score as set in the leaderboard.
data[i].details (string) – Details of the entry. String is used as a byte array, so may contain a
'\0'
in the middle.data[i].UGC (uint64) – Handle for the UGC attached to the entry. A special value if there is none.
err (boolean): true if there was any IO error with the request.
Warning
This function has two major differences from the SteamWorks API.
If the data request is “Friends”, you must not use the rangeStart and rangeEnd parameters (see the second example).
The callback is not called with a table similar to LeaderboardScoresDownloaded_t, and there is no need to use the function GetDownloadedLeaderboardEntry, since this is already done for you. The callback already receives a list of objects like LeaderboardEntry_t.
Examples:
function showGlobalEntries(handle)
Steam.userStats.downloadLeaderboardEntries(handle, 'Global', 1, 1000, function(data, err)
if err then
print('Error happened')
else
for _, user in ipairs(data) do
print('Rank #' .. user.globalRank .. ': ' .. user.score)
end
end
end
end
function showFriendsEntries(handle)
Steam.userStats.downloadLeaderboardEntries(handle, 'Friends', function(data, err)
if err then
print('Error happened')
else
for _, user in ipairs(data) do
local name = Steam.friends.getFriendPersonaName(user.steamIDUser)
print('Friend ' .. name .. ': ' .. user.score)
end
end
end
end
Callbacks Reference¶
Warning
Remember callbacks are functions that you should override in order to receive the events, and not call directly.
Also, you must constantly call Steam.runCallbacks()
(preferably in your game loop) in order for your callbacks to be called.
- userStats.onUserStatsReceived(data)¶
- Parameters:
data (table) –
A table similar to UserStatsReceived_t
data.gameID (uint64) – Game ID that these stats are for.
data.result (int) – Returns whether the call was successful or not. If the user has no stats, this will be set to 2.
data.steamIDUser (uint64) – The user whose stats were retrieved.
- Returns:
nothing
- SteamWorks:
Called when the latest stats and achievements for a specific user (including the local user) have been received from the server.
Example:
function Steam.userStats.onUserStatsReceived(data)
print('Result: ' .. data.result)
end