[SOLVED]How to add a delegate to the Begin/End ComponentOverlap?

This is my header

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FPS_Character.generated.h"

class UCameraComponent;

UCLASS()
class BASIC_SHOOTER_01_API AFPS_Character : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AFPS_Character();

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

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

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	
private:
	bool isTouchingGround = false;

private:
	UCameraComponent* ptrCamera = nullptr;

private:
	void Jump();

	UFUNCTION()
	void OnCollisionEnter
					(
						const FOverlapInfo& OtherOverlap,
						bool bDoNotifies
					);

	UFUNCTION()
	void OnCollisionExit
					(
						const FOverlapInfo& OtherOverlap,
						bool bDoNotifies,
						bool bSkipNotifySelf
					);
};

This is my CPP file

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

#include "FPS_Character.h"
#include "Camera/CameraComponent.h"
#include "Engine.h"
#include "Components/CapsuleComponent.h"


// Sets default values
AFPS_Character::AFPS_Character() :
	ptrCamera { CreateDefaultSubobject<UCameraComponent>(TEXT("FPS_camera")) }
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	ptrCamera->SetupAttachment(this->GetCapsuleComponent());
	ptrCamera->RelativeLocation = FVector(-39.56f, 1.75f, 64.f);

	this->GetCapsuleComponent()->BeginComponentOverlap.AddDynamic(this, &AFPS_Character::OnCollisionExit);
	this->GetCapsuleComponent()->EndComponentOverlap.AddDynamic(this, &AFPS_Character::OnCollisionEnter);
}

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

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

}

// Called to bind functionality to input
void AFPS_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

void AFPS_Character::Jump()
{
}

void AFPS_Character::OnCollisionEnter
(
	const FOverlapInfo& OtherOverlap, 
	bool bDoNotifies
)

{
	if (OtherOverlap.OverlapInfo.GetActor() != this)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("You collided with: %s"), OtherOverlap.OverlapInfo.GetActor()->GetName()));
	}
}

void AFPS_Character::OnCollisionExit
(
	const FOverlapInfo& OtherOverlap, 
	bool bDoNotifies, 
	bool bSkipNotifySelf
)

{
	if (OtherOverlap.OverlapInfo.GetActor() != this)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("You uncollided with: %s"), OtherOverlap.OverlapInfo.GetActor()->GetName()));
	}
}

When I compile it says: “Unrecognized type ‘FOverlapInfo’ - type must be a UCLASS, USTRUCT or UENUM”.

How can I fix this?

from PrimitiveComponent.h

/** Delegate for notification of start of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_SixParams( FComponentBeginOverlapSignature, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);

/** Delegate for notification of end of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams( FComponentEndOverlapSignature, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex);

Those are the function signatures your functions need. I would also recommend binding the functions during begin play but it may work in the constructor.

that’s the correct answer, and yep, i definitely recommend to bind delegates in BeginPlay instead if ctor :slight_smile:

Yep! Thank you for helping me, it works!
But I wonder why is it bad to bind them in the c-tor, is it unreliable?

it’s more of a good habits, Because it would only work for component created in c++ ctor ( so for a char it’s good ), all other components ( the one you add in bps ) are not loaded yet, in begin play you are sure it’s all good ( or in ComponentsPostInitialize)