Revision Difference
Leaderboards#526675
<cat>code.general</cat>
<title>Leaderboards</title>
# Creating a Leaderboard
Creating a leaderboard is really easy.
```
using Steamworks;
using Steamworks.Data;
...
var leaderboard = await SteamUserStats.FindOrCreateLeaderboardAsync( "MyLeaderboard",
LeaderboardSort.Ascending,
LeaderboardDisplay.Numeric );
```
Here you've created a leaderboard named "MyLeaderboard" which is sorted in ascending (highest first) and is number based (high score etc) rather than time based (fastest lap etc).
# Getting a Leaderboard
You can safely get a leaderboard with the same function. If you only want to get one that exists you can use:
```
var leaderboard = await SteamUserStats.FindLeaderboardAsync( "MyLeaderboard" );
```
<note>FindLeaderboardAsync and FindOrCreateLeaderboardAsync return a nullable. If for whatever reason the board cannot be found - it'll be null. This means you should check it first via `leaderboard.HasValue` and then the real leaderboard is in `leaderboard.Value`.</note>
# Submitting a Score
```
var result = await lb.SubmitScoreAsync( 576 );
```
This function will only replace your last score if the new one is better. On success (`result.HasValue`) this function will return a <page>Data.LeaderboardUpdate</page>.
You can force your score to be replace, even if it's worse, using:
```
var result = await lb.ReplaceScoreAsync( 576 );
```
# Getting Scores
```
// Get top 20 scores
var globalScores = await lb.GetScoresAsync( 20 );
var globalScores = await lb.Value.GetScoresAsync( 20 );
foreach ( var e in globalScores)
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
// Get scores from friends
var friendScores = await lb.GetScoresFromFriendsAsync();
var friendScores = await lb.Value.GetScoresFromFriendsAsync();
foreach ( var e in friendScores )
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
// Get scores around current user
var surroundScores = await lb.GetScoresAroundUserAsync( -10, 10 );
var surroundScores = await lb.Value.GetScoresAroundUserAsync( -10, 10 );
foreach ( var e in surroundScores )
{
Console.WriteLine( $"{e.GlobalRank}: {e.Score} {e.User}" );
}
```