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 WindowThe Beamable "Leaderboard Flow" UI in the Unity Game Window

The Beamable "Leaderboard Flow" UI in the Unity Game Window


Follow these steps to get started:



  1. Setup a traditional "Leaderboard Flow" Prefab

• See Leaderboards - Guide for more info

  1. Submit Leaderboard Score w/ Additional Stats

• See Send Score below

  1. Render Leaderboard Score w/ Additional Stats

• 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


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


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 
    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(
    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, 

    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;

Did this page help you?