Overlap events in c++

So I want to create an overlap event with an actor. I’ve followed several tutorials but I still get a few errors. Can someone tell me what I’m doing wrong? Here’s the header file.

   #pragma once
    
    #include "GameFramework/Actor.h"
    #include "MyActor.generated.h"
    
    UCLASS()
    class PROJECT_API AMyActor : public AActor
    {
    	GENERATED_BODY()
    
    	UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule")
    		class UCapsuleComponent* Trigger;
    
    public:	
    	// Sets default values for this actor's properties
    	AMyActor();
    
    	// Called when the game starts or when spawned
    	virtual void BeginPlay() override;
    	
    	// Called every frame
    	virtual void Tick( float DeltaSeconds ) override;
    
    	float SphereRadius;
    
    	UPROPERTY(EditAnywhere)
    		UStaticMeshComponent* Mesh;
    
    	UFUNCTION()
    		void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    };

And here’s the cpp file:

// Fill out your copyright notice in the Description page of Project Settings.

#include "Project.h"
#include "MyActor.h"
#include "Components/CapsuleComponent.h"
#include "DrawDebugHelpers.h"



// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>("VCone1");

	// declare trigger capsule
	Trigger = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
	Trigger->InitCapsuleSize(55.f, 96.0f);;
	Trigger->SetCollisionProfileName(TEXT("Trigger"));
	Trigger->SetupAttachment(RootComponent);

	Trigger->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);

}


void AMyActor::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Overlap Begin"));
	}
}


// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

The first error I’m getting is with Trigger->SetupAttachment(RootComponent);. It keeps saying that UCapsuleComponent has no member named SetupAttachment. The second problem is with the line Trigger->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);. Does anyone know what the problem is?

1 Like

Hey, is it visual studio intellisense error ? or build error( output pan) ?

Just like that i don’t see obvious errors in your code
try to build and past the output log here

You have a syntax error on line 20. Remove the extra semicolon.

Even though It should probably still run, if not, instead of using SetupAttachment, can you try this and see if it compiles:

RootComponent = Trigger;

Here’s the errors I’m getting:

CompilerResultsLog:Error: Error C:\Users\Brandon\Desktop\CSC473_assignment2\Source\CSC473_assignment2\MyActor.cpp(25) : error C2664: 'void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &>::__Internal_AddDynamic<AMyActor>(UserClass *,void (__cdecl AMyActor::* )(AActor *,UPrimitiveComponent *,int32
,bool,const FHitResult &),FName)': cannot convert argument 2 from 'void (__cdecl AMyActor::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)' to 'void (__cdecl AMyActor::* )(AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)'

I’m still getting an error pertaining to line 24:

CompilerResultsLog:Error: Error C:\Users\Brandon\Desktop\CSC473_assignment2\Source\CSC473_assignment2\MyActor.cpp(25) : error C2664: 'void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &>::__Internal_AddDynamic<AMyActor>(UserClass *,void (__cdecl AMyActor::* )(AActor *,UPrimitiveComponent *,int32
,bool,const FHitResult &),FName)': cannot convert argument 2 from 'void (__cdecl AMyActor::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)' to 'void (__cdecl AMyActor::* )(AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)'

This is very odd, I do not see any issues, so I took your code and it compiles for me with no errors. But then I noticed your post says you are using engine version 4.11. If you are, that would definitely be your problem. This is current engine code your using. In engine 4.11 the overlap function most likely has a different code signature. I would recommend updating to 4.20 and this should work. If not, you will need to use the 4.11 version of the signature by finding the delegate inside the UPrimitiveComponent class. (Which it looks like the compiler gave you the answer.)

So change your signature for your OnOverlapBegin function in your header file and cpp file to this:

(AActor* OtherActor, UPrimitiveComponent* OverlappedComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)

Indeed, as said Steve, you are using 4.11 and from the log and the 4.11 source the overlap singature is
(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

so you need to have :

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

void AMyActor::OnOverlapBegin(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)