#include "CollectiblesWidget.h" void UCollectiblesWidget::UpdateCollectible(AActor* CollectableActor, bool bIsCollected) { if (CollectibleActorToIndexMap.Contains(CollectableActor)) { const int IconIndex = CollectibleActorToIndexMap[CollectableActor]; if (CollectibleIcons.IsValidIndex(IconIndex)) { UImage* Icon = CollectibleIcons[IconIndex]; if (bIsCollected && FilledIconTexture) { Icon->SetBrushFromTexture(FilledIconTexture); } else if (!bIsCollected && EmptyIconTexture) { Icon->SetBrushFromTexture(EmptyIconTexture); } } } } void UCollectiblesWidget::NativeConstruct() { Super::NativeConstruct(); // Log UE_LOG(LogTemp, Warning, TEXT("UCollectiblesWidget::NativeConstruct()")); PlayerScoreActorComponent = Cast(GetOwningPlayer()->GetPawn()->GetComponentByClass(UPlayerScoreActorComponent::StaticClass())); if (PlayerScoreActorComponent) { FTimerHandle TimerHandle; GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &UCollectiblesWidget::PopulateCollectibleIcons, 3.0f, false); } } void UCollectiblesWidget::InitializeCollectibles() { PopulateCollectibleIcons(); if (PlayerScoreActorComponent) { PlayerScoreActorComponent->OnCollectablePickedUp.AddDynamic(this, &UCollectiblesWidget::UpdateCollectible); } } void UCollectiblesWidget::PopulateCollectibleIcons() { for (UWidget* Widget : CollectiblesHorizontalBox->GetAllChildren()) { if (UImage* ImageWidget = Cast(Widget)) { CollectibleIcons.Add(ImageWidget); // Store reference to each image widget } } int Index = 0; for (const TPair& Collectible : PlayerScoreActorComponent->GetCollectibles()) { if (CollectibleIcons.IsValidIndex(Index)) { AActor* CollectableActor = Collectible.Key; CollectibleActorToIndexMap.Add(CollectableActor, Index); // Map collectible actor to its corresponding icon index Index++; } } }