How to override OnActorBeginOverlap?

I try to override OnAcotrBeginOverlap.
Zone is parent class and DangerZone is child class.
I want my child class do the parent’s OnActorBeginOverlapEvent and also his.
the problem is, child class excutes parent’s function only.

this is Zone.cpp

void AZone::BeginPlay()
{
	Super::BeginPlay();
	CollisionMesh->OnComponentBeginOverlap.AddDynamic(this, &AZone::OnActorBeginOverlap);
}

void AZone::OnActorBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%f"), eZone));
}

this is DangerZone.cpp

void ADangerZone::OnActorBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Super::OnActorBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("sdfsdfsdf%f"), eZone));
}

void ADangerZone::BeginPlay()
{
	CollisionMesh->OnComponentBeginOverlap.AddDynamic(this, &ADangerZone::OnActorBeginOverlap);
}

Hello! Can you share header files? Are those functions marked as virtual?

Hi,

what you are doing here, is to bind a function to OnComponentBeginOverlap. Something extremely confusing – you are naming that function in the same way a native event is called – that is “OnActorBeginOverlap”. I’d start by creating your own function to bind OnComponentBeginOverlap to. For example, let’s call it simply onOverlap; it has to be a UFUNCTION otherwise you can’t bind it with AddDynamic. That alone makes everything works. I did actually test it and… it works indeed. Code below.

Cheers
f

in zone.h

virtual void PostInitializeComponents() override;

UFUNCTION()
virtual void onOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

UPROPERTY(EditAnywhere)
UStaticMeshComponent* CollisionMesh;

in Zone.cpp

void AZone::PostInitializeComponents()
{
	Super::PostInitializeComponents();
	CollisionMesh->OnComponentBeginOverlap.AddDynamic(this, &AZone::onOverlap);
}

void AZone::onOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("This is the Zone")));
}

in Dangerzone.h

// note there's no need of UFUNCTION as it has that specifier already in its base definition
virtual void onOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;

in Dangerzone.cpp

void ADangerZone::onOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Super::onOverlap(OverlappedComponent, OtherActor, OtherComp,  OtherBodyIndex, bFromSweep,  SweepResult);
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("This is the Danger Zone")));
}
1 Like

Everything works correctly.
I have another question, the “PostInitializeComponents()”.
I haven’t see this before. Is that function called before construction?

Technically, just after construction. Here’s the breakdown showing you an Actor Lifecycle:

In a nutshell, afaiu you call PostInitializeComponents() when you want to perform actions that depends on the components of your actor having being initialized, and that you actor needs before being spawned (for things to do as-you-spawn there’s BeginPlay)