#include "ScoreActorComponent.h" // Sets default values for this component's properties UScoreActorComponent::UScoreActorComponent() { // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. PrimaryComponentTick.bCanEverTick = true; Score = 0; bShouldApplyScore = true; // ... } // Called when the game starts void UScoreActorComponent::BeginPlay() { Super::BeginPlay(); if (APlayerController* PlayerController = GetWorld()->GetFirstPlayerController()) { if (APawn* PlayerPawn = PlayerController->GetPawn()) { PlayerScoreActorComponent = PlayerPawn->FindComponentByClass(); if (!PlayerScoreActorComponent) { UE_LOG(LogTemp, Warning, TEXT("PlayerScoreActorComponent not found!")); } } } } void UScoreActorComponent::EndPlay(const EEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); if (EndPlayReason == EEndPlayReason::LevelTransition || EndPlayReason == EEndPlayReason::RemovedFromWorld) { return; } if (bShouldApplyScore) { if (APlayerController* PlayerController = GetWorld()->GetFirstPlayerController()) { if (APawn* PlayerPawn = PlayerController->GetPawn()) { PlayerScoreActorComponent = PlayerPawn->FindComponentByClass(); if (!PlayerScoreActorComponent) { UE_LOG(LogTemp, Warning, TEXT("PlayerScoreActorComponent not found!")); return; } else { ApplyScore(); } } } } else { return; } } // Called every frame void UScoreActorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); // ... } void UScoreActorComponent::ApplyScore() { PlayerScoreActorComponent->ScoreToBeChanged.Broadcast(); // Delay the AddScore so that animation can play FTimerHandle TimerHandle; GetWorld()->GetTimerManager().SetTimer(TimerHandle, [this]() { // Get player in the world if (PlayerScoreActorComponent && PlayerScoreActorComponent->GetOwner()) { PlayerScoreActorComponent->AddScore(this); } }, 0.15f, false); }