# How to override OnActorBeginOverlap?

**URL:** <https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585>\
**Category:** Community & Industry Discussion\
**Tags:** override, overlap, unreal-engine, question\
**Created:** [March 1, 2021, 8:38am UTC](https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585 "2021-03-01T08:38:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_03f0a1364975325bb3762b9b0e48114788315f551ac1f0f104b5b5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_03f0a1364975325bb3762b9b0e48114788315f551ac1f0f104b5b5](https://forums.unrealengine.com/u/user_03f0a1364975325bb3762b9b0e48114788315f551ac1f0f104b5b5)\
**Post date:** [March 1, 2021, 8:38am UTC](https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585/1 "2021-03-01T08:38:06Z")

</div>

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);
}

```

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [March 1, 2021, 4:20pm UTC](https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585/2 "2021-03-01T16:20:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fraps2020](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fraps2020/32/460865_2.png) [@Fraps2020](https://forums.unrealengine.com/u/Fraps2020)\
**Post date:** [March 2, 2021, 8:43pm UTC](https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585/3 "2021-03-02T20:43:31Z")

</div>

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")));
}
```

---

<div class="post-metadata">

**Author:** ![user\_03f0a1364975325bb3762b9b0e48114788315f551ac1f0f104b5b5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_03f0a1364975325bb3762b9b0e48114788315f551ac1f0f104b5b5](https://forums.unrealengine.com/u/user_03f0a1364975325bb3762b9b0e48114788315f551ac1f0f104b5b5)\
**Post date:** [March 6, 2021, 1:17pm UTC](https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585/4 "2021-03-06T13:17:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fraps2020](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fraps2020/32/460865_2.png) [@Fraps2020](https://forums.unrealengine.com/u/Fraps2020)\
**Post date:** [March 8, 2021, 11:45am UTC](https://forums.unrealengine.com/t/how-to-override-onactorbeginoverlap/479585/5 "2021-03-08T11:45:21Z")

</div>

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

> **[Actor Lifecycle](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Actors/ActorLifecycle/)**
>
> What actually happens when an Actor is loaded or spawned, and eventually dies.

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)
