Begin Overlap in C++

Hi community

I am trying to make an actor be able to fire off an ActorBeginOverlap event in c++. I keep getting errors and due to my inexperience with c++, I am unable to identify what needs fixing and how to fix it.

My code is below:

My c++ file

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

#include "PlatformTrigger.h"
#include "Components/BoxComponent.h"

// Sets default values
APlatformTrigger::APlatformTrigger()
{
 	// 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;

	TriggerVolume = CreateDefaultSubobject<UBoxComponent>(FName("TriggerVolume"));
	
	if (!ensure(TriggerVolume != nullptr)) return;

	RootComponent = TriggerVolume;

	TriggerVolume->OnComponentBeginOverlap.AddDynamic(this, &APlatformTrigger::OnOverlapBegin);


}

// Called when the game starts or when spawned
void APlatformTrigger::BeginPlay()
{


	Super::BeginPlay();
	
}

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

}

void APlatformTrigger::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
{
	UE_LOG(LogTemp, Warning, TEXT("Activated"));

}

And my header file

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PlatformTrigger.generated.h"

UCLASS()
class PUZZLEPLATFORM_API APlatformTrigger : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	APlatformTrigger();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

private:
	UPROPERTY(VisibleAnywhere)
	class UBoxComponent* TriggerVolume;

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

press post the errors you are getting, so we can help.

I would have posted the errors I am getting, if I could have uploaded them at the time of uploading this question

Here is the log message

Alright. I have added the screenshots of the errors

Try declaring your overlap function like this:

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

It can’t find the definition of OnOverlapBegin I believe it’s because you have placed curly braces around the definition, I have never seen that syntax before so I am assuming that’s the problem.

1 Like

I did this in the header file and it gave me the error “Function definition for “OnOverlapBegin” not found” in the header file
And the c++ file was “declaration is incompatible”

You are gonna have to send me the errors again and all the the output.

.h

class UBoxComponent* Box

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

.cpp
 
Box=CreateDefaultSubobject<UBoxComponent>(TEXT("Box"));

Box->OnComponentBeginOverlap.AddDynamic(this, &AYourClass::OnBoxBeginOverlap);

if it does not work you need to first reload visual studio from editor (file menu).

restarting your project can always make everything work better :))

4 Likes

I just noticed this, remove the semi-colon on the end of line 39 and give it a try

That was one of the issues I had and after removing the semi colon and the resolved answer, it worked

This worked. Thank you so much