メインコンテンツまでスキップ

すべてを統合する - 全期間のリーダーボード - (Unreal Engine モジュール)

Last updated on May 30, 2024

Connect the UI to display leaderboard rankings

  1. Open the LeaderboardAllTimeWidget_Starter class CPP file, navigate to the GetRankings() function, and replace the current code with the code below. It will get the leaderboard rankings using the LeaderboardSubsystem_Starter and then display it upon completion.

    void ULeaderboardAllTimeWidget_Starter::GetRankings()
    {
    FUniqueNetIdRepl PlayerNetId = GetOwningPlayer()->GetLocalPlayer()->GetPreferredUniqueNetId();
    if (!PlayerNetId.IsValid())
    {
    return;
    }

    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);

    LeaderboardSubsystem->GetRankings(
    GetOwningPlayer(),
    LeaderboardCode,
    ResultLimit,
    FOnGetLeaderboardRankingComplete::CreateWeakLambda(this, [this, PlayerNetId](bool bWasSuccessful, const TArray<ULeaderboardRank*> Rankings)
    {
    if (!bWasSuccessful)
    {
    Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
    return;
    }

    // Add rankings to the leaderboard ranking list.
    Lv_Leaderboard->SetListItems(Rankings);

    // Get the logged-in player's rank if it is not included in the leaderboard.
    const TArray<ULeaderboardRank*> FilteredRank = Rankings.FilterByPredicate([PlayerNetId](const ULeaderboardRank* Temp) { return Temp && Temp->UserId == PlayerNetId; });
    const ULeaderboardRank* PlayerRank = FilteredRank.IsEmpty() ? nullptr : FilteredRank[0];
    if (!PlayerRank)
    {
    GetPlayerRanking();
    }
    // Display the rankings if it is not empty.
    else
    {
    DisplayPlayerRank(PlayerRank);
    Ws_Leaderboard->SetWidgetState(
    Lv_Leaderboard->GetNumItems() <= 0 ?
    EAccelByteWarsWidgetSwitcherState::Empty :
    EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    }
    ));
    }
  2. Navigate to GetPlayerRanking() and replace the current code with the code below. It will only be called if the currently logged-in player is not included in the rankings retrieved by the GetRankings() function.

    void ULeaderboardAllTimeWidget_Starter::GetPlayerRanking()
    {
    LeaderboardSubsystem->GetPlayerRanking(
    GetOwningPlayer(),
    LeaderboardCode,
    FOnGetLeaderboardRankingComplete::CreateWeakLambda(this, [this](bool bWasSuccessful, const TArray<ULeaderboardRank*> Rankings)
    {
    // Get and display the logged-in player's rank.
    DisplayPlayerRank((!bWasSuccessful || Rankings.IsEmpty()) ? nullptr : Rankings[0]);

    // Display the rankings if it is not empty.
    Ws_Leaderboard->SetWidgetState(
    Lv_Leaderboard->GetNumItems() <= 0 ?
    EAccelByteWarsWidgetSwitcherState::Empty :
    EAccelByteWarsWidgetSwitcherState::Not_Empty);
    }
    ));
    }

Resources