#include "LeaderboardUserWidget.h" #include "Kismet/GameplayStatics.h" void ULeaderboardUserWidget::DisplayLeaderboard() { // Check if save file exists if (UGameplayStatics::DoesSaveGameExist(TEXT("CustomSaveSlot"), 0)) { CustomGameInstance->SaveGameInstance = Cast(UGameplayStatics::LoadGameFromSlot(TEXT("CustomSaveSlot"), 0)); CustomGameInstance->LoadLeaderboard(); } else { CustomGameInstance->SaveGameInstance = Cast(UGameplayStatics::CreateSaveGameObject(UCustomSaveGame::StaticClass())); } LeaderboardVerticalBox->ClearChildren(); // If leaderboard is empty; create a character score and add it to the leaderboard if (CustomGameInstance->LeaderboardInstance->Leaderboard.Num() == 0) { FCharacterScore NewScore; NewScore.Name = "Evil Orca"; NewScore.Score = 999; CustomGameInstance->LeaderboardInstance->AddCharacterScore(NewScore); CustomGameInstance->SaveLeaderboard(); } // Get leaderboard from game instance LeaderboardCopy = CustomGameInstance->LeaderboardInstance; LeaderboardCopy->SortLeaderboard(); TopScores = LeaderboardCopy->Leaderboard; // Limit the number of scores to display if (TopScores.Num() > NumScoresToDisplay) { TopScores.SetNum(NumScoresToDisplay); } for (int i = 0; i < TopScores.Num(); i++) { // Duplicate the template HorizontalBox UHorizontalBox* NewScoreEntry = NewObject(this); // Rank UTextBlock* RankTextBlock = NewObject(this); RankTextBlock->SetText(FText::FromString(FString::Printf(TEXT("%d"), i + 1))); RankTextBlock->SetFont(TB_RankTemplate->GetFont()); if (const UHorizontalBoxSlot* RankSlot = Cast(TB_RankTemplate->Slot)) { UHorizontalBoxSlot* NewRankSlot = NewScoreEntry->AddChildToHorizontalBox(RankTextBlock); // Padding and alignment NewRankSlot->SetPadding(RankSlot->GetPadding()); NewRankSlot->SetHorizontalAlignment(RankSlot->GetHorizontalAlignment()); } // Name UTextBlock* NameTextBlock = NewObject(this); NameTextBlock->SetText(FText::FromString(TopScores[i].Name)); NameTextBlock->SetFont(TB_NameTemplate->GetFont()); if (const UHorizontalBoxSlot* NameSlot = Cast(TB_NameTemplate->Slot)) { UHorizontalBoxSlot* NewNameSlot = NewScoreEntry->AddChildToHorizontalBox(NameTextBlock); // Padding and alignment NewNameSlot->SetPadding(NameSlot->GetPadding()); NewNameSlot->SetHorizontalAlignment(NameSlot->GetHorizontalAlignment()); } // Score UTextBlock* ScoreTextBlock = NewObject(this); ScoreTextBlock->SetText(FText::FromString(FString::Printf(TEXT("%d"), TopScores[i].Score))); ScoreTextBlock->SetFont(TB_ScoreTemplate->GetFont()); if (const UHorizontalBoxSlot* ScoreSlot = Cast(TB_ScoreTemplate->Slot)) { UHorizontalBoxSlot* NewScoreSlot = NewScoreEntry->AddChildToHorizontalBox(ScoreTextBlock); // Padding and alignment NewScoreSlot->SetPadding(ScoreSlot->GetPadding()); NewScoreSlot->SetHorizontalAlignment(ScoreSlot->GetHorizontalAlignment()); } // Add the constructed HorizontalBox to the VerticalBox LeaderboardVerticalBox->AddChild(NewScoreEntry); } ScoreEntryHorizontalBoxTemplate->SetVisibility(ESlateVisibility::Collapsed); } void ULeaderboardUserWidget::NativeConstruct() { Super::NativeConstruct(); if (UCustomGameInstance* GameInstance = Cast(UGameplayStatics::GetGameInstance(GetWorld()))) { CustomGameInstance = GameInstance; } DisplayLeaderboard(); }