Leaderboards - Code
Allow player to manage a leaderboard with animated text [SOCL-Leaderboards-03]
Variations On A Theme
Choose the Leaderboard setup that works best for your needs;
• Leaderboard (Standard) - Shows non-animated score values. See Leaderboards - Guide for more info
• Leaderboard (With Interpolation) - Shows animated score values. See Leaderboards - Code for more info
The purpose of this guide is for game makers to setup a LeaderboardLeaderboard - A list of high scores by all players of a game with interpolation.
Here, "interpolation" refers to a LeaderboardLeaderboard - A list of high scores by all players of a game with animated score
text. The score value rises creating excitement in games with rapidly changing scores.
The User Interface
When set up properly, the player's user interface in the game project will be as follows:
Animated Screenshot


The Beamable "Leaderboard Flow" UI in the Unity Game Window
Steps
Follow these steps to get started:
Step | Detail |
---|---|
| • See Leaderboards - Guide for more info |
| • See Send Score below |
| • See Render Score below Note: The provided example is built on top of the existing "Leaderboard Flow" Prefab. This has limited flexibility. Depending on the needs of the project, game makers may choose instead to build a custom Leaderboard UI from scratch. In either case the provided example serves as a useful model |
Example
Here are examples which cover common programming needs.
Beamable SDK Examples
• The following example code is available for download at GitHub.com/Beamable_SDK_Examples
Code
Submit Score
Here is a snippet from LeaderboardFlowInterpolationExample.cs
;
private async void SetupBeamable()
{
_beamContext = BeamContext.Default;
await _beamContext.OnReady;
Debug.Log($"_beamContext.PlayerId = {_beamContext.PlayerId}");
LeaderboardContent leaderboardContent =
await _leaderboardMainMenuCustom.LeaderboardBehavior.Leaderboard.Resolve();
Debug.Log($"PopulateLeaderboard Starting. Wait < 30 seconds... ");
int leaderboardRowCountMin = 10;
int leaderboardScoreMin = 99;
int leaderboardScoreMax = 99999;
// Populate with custom values
Dictionary<string, object> leaderboardStats = new Dictionary<string, object>();
leaderboardStats.Add("leaderboard_score_timestamp", new
DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds());
leaderboardStats.Add("leaderboard_score_velocity", 99); // 99 score delta per second
// Populates mock "alias" and "score" for each leaderboard row
string loggingResult = await MockDataCreator.PopulateLeaderboardWithMockData(
_beamContext,
leaderboardContent,
leaderboardRowCountMin,
leaderboardScoreMin,
leaderboardScoreMax,
leaderboardStats);
Debug.Log($"PopulateLeaderboard Finish. Result = {loggingResult}");
}
Render Score
Here is a snippet from LeaderboardItemCustom.cs
;
public void Update()
{
// Prepare value
long scoreTimestamp = long.Parse(_rankEntry.GetStat("leaderboard_score_timestamp"));
long scoreVelocity = long.Parse(_rankEntry.GetStat("leaderboard_score_velocity"));
long currentTimestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
long millisecondsSinceSubmission = currentTimestamp - scoreTimestamp;
long scoreSinceSubmission = (millisecondsSinceSubmission * scoreVelocity) / 1000;
// Render value
double score = _rankEntry.score + scoreSinceSubmission;
TxtScore.text = $"{score:00000}";
}
Get Board
Here is a snippet from LeaderboardServiceExample.cs
;
private async Task<List<RankEntry>> LeaderboardServiceGetBoard(string id, long userId)
{
LeaderBoardView leaderBoardView = await _beamContext.Api.LeaderboardService.GetBoard(id, 0, 100,
userId);
foreach (RankEntry rankEntry in leaderBoardView.rankings)
{
// Get alias for userId of rankEntry
long nextUserId = rankEntry.gt;
var stats =
await _beamContext.Api.StatsService.GetStats("client", "public", "player", nextUserId );
string alias = "";
stats.TryGetValue(alias, out alias);
if (string.IsNullOrEmpty(alias))
{
alias = "Unknown Alias";
}
// Log
Debug.Log($"Rank = {rankEntry.rank}, Alias = {alias}, Score = {rankEntry.score}");
}
return leaderBoardView.rankings;
}
Updated about 1 month ago