C++ Delegate Not Being Called

I am learning how to use C++ with UE5, and have been stuck on a problem with declaring and calling delegates.

The basis of what I am trying to accomplish is I have an Actor which collides with a UBoxComponent, which then broadcasts that delegate which can be accessed from other classes.

I declare the collider as such in my header file:
image

And the delegates are declared above the UCLASS declaration like so:
image
And then assigned to variables where I define all of my UPROPERTIES:
image

I also declare my OnOverlapBegin function which is what I am trying to call on overlap:
image

In my cpp file I assign the variables as shown:

And lastly I bind the colliders to the delegate:

I have a blueprint child class placed in the editor, and when a collision occurs nothing happens. From my understanding, the OnOverlapBegin function is not being called at all, since the print statement is not printed to the console.

In the blueprint, all of the box colliders have the OverlapAllDynamic Collision preset.

Thank you in advance for any of your help. If more information is needed feel free to ask.

A delegate to work in the way that you can bind to it (subscribing the chosen actor to react to the delegate).
This is done by referencing the delegate actor inside the one that is supposed to receive the message and using bind on it.

Once you have bound objects (actors, characters etc) then you can call Broadcast on the delegate where it originates.
This will send a message to all subscribed actors.

In your case you are registering the overlap event delegate on your colliders.
Do they have the appropriate settings to receive overlap events?

UBoxComponent * TopCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("TopCollider"));
TopCollider->SetGenerateOverlapEvents(true);
// also set collision profile
TopCollider->SetCollisionProfileName(FName("OverlapAll"));

If you want to use the signature delegates then you would need to call broadcast on them inside the overlap function.

So

a) Bind overlap delegate to call function
b) inside overlap event call broadcast on signature delegate

void AABoundry::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {	
	OnWallCollision.Broadcast();
}

c) inside receiving actor bind to signature delegate of the actor from a
d) bound function in receiving actor should be called on overlap that occurs in actor from a.

P.S
if you want to be able to bind the signature delegate inside of blueprints later on then remember to put BlueprintAssignable inside of it’s UPROPERTY()

eg.

	UPROPERTY(BlueprintReadWrite, editAnywhere, BlueprintAssignable)
		FSignatureDelegate overlapDel;

I created a new third person cpp project using Unreal 5.0.3 and created a new class, derived from AActor called MyActor. Here is the cpp/h for this class:

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


#include "MyActor.h"
#include "Components/BoxComponent.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;

	DefaultSceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneComponent"));
	SetRootComponent(DefaultSceneComponent);

	AIGoalCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("AIGoalCollider"));
	AIGoalCollider->SetupAttachment(RootComponent);

	TopCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("TopCollider"));
	TopCollider->SetupAttachment(RootComponent);

	BottomCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("BottomCollider"));
	BottomCollider->SetupAttachment(RootComponent);

	PlayerGoalCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("PlayerGoalCollider"));
	PlayerGoalCollider->SetupAttachment(RootComponent);

}

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

void AMyActor::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	AIGoalCollider->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
	TopCollider->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
	BottomCollider->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
	PlayerGoalCollider->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
}

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

}

void AMyActor::OnOverlapBegin(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Warning, TEXT("OVERLAP: %s"), *HitComp->GetName());
}
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWallCollisionSignature);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnAIGoalCollisionSignature);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPlayerGoalCollisionSignature);

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

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

	virtual void PostInitializeComponents() override;

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

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

public:
	
	UPROPERTY(EditAnywhere, Category = "Component")
	class USceneComponent* DefaultSceneComponent;

	UPROPERTY(EditAnywhere, Category = "Component")
	class UBoxComponent* AIGoalCollider;

	UPROPERTY(EditAnywhere, Category = "Component")
	class UBoxComponent* TopCollider;

	UPROPERTY(EditAnywhere, Category = "Component")
	class UBoxComponent* BottomCollider;

	UPROPERTY(EditAnywhere, Category = "Component")
	class UBoxComponent* PlayerGoalCollider;

	UPROPERTY(BlueprintAssignable)
	FOnWallCollisionSignature OnWallCollision;

	UPROPERTY(BlueprintAssignable)
	FOnAIGoalCollisionSignature OnAIGoalCollision;

	UPROPERTY(BlueprintAssignable)
	FOnPlayerGoalCollisionSignature OnPlayerGoalCollision;

};

To differentiate these box components, I gave them slight offsets in the derived blueprint as shown below:

With this code, and when using PIE, I get the result shown in the attached video:

A potential guess as to why my example works and yours does not is where you are binding the delegates. From my experience, these component delegate bindings are best done in the function PostInitializeComponents(), which is found in the AActor class that you can then override in your class. Binding these delegates in the constructor can cause issues or even compile errors.

I hope this information helps, and if there are still issues, please let me know :slight_smile: Good luck!

Thanks so much for the help! I was able to move my AddDynamic calls into the PostInitialization function and that seems to have solved it! Hope you both had a great new years!

2 Likes