Skip to main content

Put it all together - All time leaderboard - (Unity module)

Last updated on June 23, 2025

Connect the UI to Display Leaderboard Rankings

  1. Open the LeaderboardAllTimeMenu_Starter class and define a variable to store the wrapper you created.

    private LeaderboardEssentialsWrapper_Starter leaderboardWrapper;
  2. Next, replace the OnEnable() function with the code below. It initializes the wrapper using the LeaderboardEssentialsWrapper_Starter class and will only display the ranking list if the wrapper is valid.

    private void OnEnable()
    {
    leaderboardWrapper ??= TutorialModuleManager.Instance.GetModuleClass<LeaderboardEssentialsWrapper_Starter>();
    if (leaderboardWrapper)
    {
    DisplayRankingList();
    }
    }
  3. Create the helper variable below which references the leaderboard code based on the selected game mode from the LeaderboardsMenu class.

    private string LeaderboardCode => 
    LeaderboardEssentialsModels.GetLeaderboardCodeByGameMode(LeaderboardsMenu.SelectedGameMode);
  4. Next, update the DisplayRankingList() function with the code below. This function sends a request to get leaderboard rankings. After receiving the list, it queries all player information (e.g., display name) from the leaderboard list. Once done, it instantiates the leaderboard entries to be displayed.

    private void DisplayRankingList()
    {
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
    leaderboardListPanel.DestroyAllChildren();

    // Get leaderboard ranking list.
    leaderboardWrapper.GetRankings(
    LeaderboardCode,
    (Result<LeaderboardRankingResult> result) =>
    {
    // The backend treats an empty leaderboard as an error. Therefore, display the empty state instead.
    if (result.IsError || result.Value.data.Length <= 0)
    {
    BytewarsLogger.LogWarning($"Failed to display leaderboard rankings. Error: {result.Error.Message}");
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Empty);
    return;
    }

    // Query the users info in the leaderboard rankings.
    UserPoint[] rankings = result.Value.data;
    AccelByteSDK.GetClientRegistry().GetApi().GetUser().GetUserOtherPlatformBasicPublicInfo(
    "ACCELBYTE",
    result.Value.data.Select(x => x.userId).ToArray(),
    (Result<AccountUserPlatformInfosResponse> usersInfoResult) =>
    {
    // Abort if failed to query users info.
    if (usersInfoResult.IsError)
    {
    BytewarsLogger.LogWarning($"Failed to display leaderboard rankings. Error: {result.Error.Message}");
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
    return;
    }

    // Generate leaderboard ranking entries.
    int rankOrder = 0;
    bool isCurrentPlayerInRanked = false;
    Dictionary<string, AccountUserPlatformData> usersInfo = usersInfoResult.Value.Data.ToDictionary(x => x.UserId, x => x);
    foreach(UserPoint ranking in rankings)
    {
    LeaderboardEntry entry = Instantiate(leaderboardEntryPrefab, leaderboardListPanel).GetComponent<LeaderboardEntry>();
    usersInfo.TryGetValue(ranking.userId, out AccountUserPlatformData userInfo);
    entry.SetRankingDetails(ranking.userId, ++rankOrder, userInfo?.DisplayName, ranking.point);

    // If the user is the current logged-in player, display the rank on the player ranking entry card.
    if (ranking.userId == GameData.CachedPlayerState.PlayerId)
    {
    isCurrentPlayerInRanked = true;
    DisplayPlayerRanking(new UserRanking()
    {
    rank = rankOrder,
    point = ranking.point
    });
    }
    }

    // Get the logged-in player's rank if it is not included in the leaderboard.
    if (!isCurrentPlayerInRanked)
    {
    leaderboardWrapper.GetUserRanking(
    GameData.CachedPlayerState.PlayerId,
    LeaderboardCode,
    (Result<UserRankingDataV3> userRankResult) =>
    {
    // Display the leaderboard result.
    DisplayPlayerRanking(userRankResult.IsError ? null : userRankResult.Value.AllTime);
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
    });
    return;
    }

    // Display the leaderboard result.
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
    });
    },
    0, LeaderboardEssentialsModels.QueryRankingsLimit);
    }

Resources