OnActorBeginOverlap is undefined

I’m trying to use the example code from OnActorBeginOverlap | Unreal Engine Documentation as a guide to figure out how to configure a component on my Actor to listen for other Actors to enter the overlap sphere attached to him. I made a test class, UAoETester.cpp/h, and I have the following in the cpp:

	bWantsBeginPlay = true;
	PrimaryComponentTick.bCanEverTick = true;

	OnActorBeginOverlap.AddDynamic(this, &UAoETester::OnOverlapBegin);

void UAoETester::OnOverlapBegin(class AActor* OtherActor){
	UE_LOG(LogTemp, Warning, TEXT("Overlap detected!"));

I expect this to print my debug message to the output console whenever a new actor enters the sphere, but I get two errors on compile, both from the line that begins OnActorBeginOverlap:

error C2065: ‘OnActorBeginOverlap’ : undeclared identifier
error C2228: left of ‘.__Internal_AddDynamic’ must have class/struct/union

I’m assuming this means that OnActorBeginOverlap isn’t recognized, but shouldn’t it be? My contextual fill-in doesn’t have it, it does have ENGINE_ReceiveActorBeginOverlap, but I assume that’s a different thing.

What do you have for includes in your header? If OnActorBeginOverlap doesn’t appear when you begin typing it, I would guess you are missing a required include.

Ooh that’s a good thought- I only have the default includes in my header:

#include "Components/ActorComponent.h"
#include "AoETester.generated.h"

Do you know if it’s resident in a third include that isn’t used here?

I thought it might be:

#include "GameFramework/Actor.h"

but when I commented it out in my code to see, nothing happened. I have it included through other header files though, so I’d still give it a try if I were you.

Hmm, I gave it a try, but no dice- it got the same two errors as in the OP. That makes sense to me, though- unless I’m mistaken, isn’t OnActorBeginOverlap a delegate meant to intermediate between Actor and Component, so components can hear overlap events on their actor without being manually messaged?

Hmmm. When I mouseover the autofill of OnActorBeginOverlap it does say it is from actor.h, and the doc pages shows it being from GameFramework/Actor.

Did you add the include I suggested before

#include "AoETester.generated.h"

I believe that needs to be the last include.

Yea, I tried it with Actor.h included, and generated.h as the last include- for precision, let me paste the entire header file, it’s very rudimentary: just the extra include, and the function declaration:

#pragma once

#include "Components/ActorComponent.h"
#include "GameFramework/Actor.h"
#include "AoETester.generated.h"

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CODEPROJ_API UAoETester : public UActorComponent

	// Sets default values for this component's properties

	// Called when the game starts
	virtual void BeginPlay() override;
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
	void OnOverlapBegin(class AActor* OtherActor);

A noteworthy difference between your code and mine is that you are using UActorComponent, whereas I use AActor.

That may explain the different results we are seeing.

Afraid I don’t have any ideas other than that, I am not familiar with UActorComponent and I assume you require UActorComponent for what you are doing.

I think I’m using the wrong tool here, re-reading OnActorBeginOverlap’s documentation confirms that it’s supposed to be run from an Actor, not a Component. I think what I need to do to make a component react to its actor’s overlap event is run this from the root character class, and add whatever override code I have in each specific component to the OnOverlapBegin delegate.

Try using “OnComponentBeginOverlap” since you are using a component which overlaps not an actor itself.

The overlap function you would have to implement would be:

YourOverlapFunction(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)

that Method would be “hooked up” with the component which should call that method by doing this:

YourCollisionComponent->OnComponentBeginOverlap.AddDynamic(this, &AYourClass::YourOverlapFunction);

I’d like to add one more tip in the above answer. Add UFUNCTION() in header file before your function. I hope this would work fine.


 void YourOverlapFunction(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)

this is the right solution. Add a collision component to your actor, make sure you have the collision channels setup the way you want. Don’t trust the documentation too much, it is often wrong, incomplete or outdated, in my experience