Download

Delete Array Element from Delegate Function

Hey Guys,

i need some help here, my C++ is still not as good at it should be.
I have an Actor that OnOverlap stores other Actors in a TArray and removes them on EndOverlap. No problems here.

So i thought, i want to track if someone died.
So when Adding the other actor, i subscribe to the death event of that actor.
This also reports back to my original actor without any problem. But if i want to remove that actor from my array, i always get:

“Array index out of bounds: -1 from an array of size0”


void ATower::TrackedActorDied(AActor* killer, AActor* DeadActor)
{
    if (HasAuthority() == false) {
        return;
    }

    UE_LOG(LogTemp, Warning, TEXT("Removing because Dead %s"), *DeadActor->GetName())
        if (TrackedActors.Contains(DeadActor)) {
            TrackedActors.Remove(DeadActor);

            if (UHealthComponent* OtherHealthComponent = Cast<UHealthComponent>(DeadActor->GetComponentByClass(UHealthComponent::StaticClass()))) {
                OtherHealthComponent->DeathEvent.RemoveDynamic(this, &ATower::TrackedActorDied);
            }

        }

}

The Contains reports true, so i am able to access that Array, it just fails to properly remove that Actor. Any Delegate rules i am forgetting here?

Greetings

klimi15