Put it all together - All time leaderboard - (Unreal Engine module)
Last updated on October 24, 2024
Connect the UI to display leaderboard rankings
Open the
LeaderboardAllTimeWidget_Starter
class CPP file, navigate to theGetRankings()
function, and replace the current code with the code below. It will get the leaderboard rankings using theLeaderboardSubsystem_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);
}
}
));
}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 theGetRankings()
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);
}
));
}Modify the
NativeOnActivated()
by replacing it with the code below. In this code, you pass the leaderboard code based on what you configured in the Admin Portal.void ULeaderboardAllTimeWidget_Starter::NativeOnActivated()
{
// Set leaderboard code based on board-unreal-highestscore-{gamemode} format.
LeaderboardCode = FString::Printf(TEXT("board-unreal-highestscore-%s"), *ULeaderboardsWidget::GetLeaderboardGameMode());
if (FFTUEDialogueModel* FTUELeaderboard = FFTUEDialogueModel::GetMetadataById("ftue_alltime_leaderboard", FTUEDialogues))
{
FTUELeaderboard->Button1.URLArguments[1].Argument = LeaderboardCode;
}
Super::NativeOnActivated();
// Reset widgets.
PlayerRankPanel->SetVisibility(ESlateVisibility::Collapsed);
Ws_Leaderboard->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
Lv_Leaderboard->ClearListItems();
// Get leaderboard rankings.
GetRankings();
}
Resources
The files used in this tutorial section are available in the Unreal Byte Wars GitHub repository.