4.3 Add Dynamic On Component Overlap error

The new 4.3 update has been causing some issues with my code. I have an actor which consists of a UBoxComponent that should call another function on component overlap. It was working fine in Version 4.2 when I used AddDynamic to delegate from OnComponentOverlap to the function I wanted it to call, but when I converted the project to Version 4.3 I got compiler error C2664:

‘void TMulticastScriptDelegate::Add(const TScriptDelegate &)’ : cannot convert argument 1 from ‘void (__cdecl ALevelChangeDetection::* )(AActor *,UPrimitiveComponent *,int32)’ to ‘const TScriptDelegate &’

Does anyone have an idea what might be causing this, or a better way to call a function on component overlap? I didn’t see anything in the release notes that looked relevant. I also tried pasting the code into a new 4.3 project in case the conversion process was the problem, but I still got the same error.

Here is the header file:

#pragma once

#include "GameFramework/Actor.h"
#include "LevelChangeDetection.generated.h"

UCLASS()
class MYPROJECT_API ALevelChangeDetection : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "CollisionBox")
	TSubobjectPtr<UBoxComponent> CollisionBox;

	UFUNCTION()
	void OnOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
	
};

and the source file:

#include "MyProject.h"
#include "LevelChangeDetection.h"


ALevelChangeDetection::ALevelChangeDetection(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	CollisionBox = PCIP.CreateAbstractDefaultSubobject<UBoxComponent>(this, TEXT("CollisionBox"));
	RootComponent = CollisionBox;
	CollisionBox->OnComponentBeginOverlap.AddDynamic(this, &ALevelChangeDetection::OnOverlap); 
}

void ALevelChangeDetection::OnOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
//do stuff
}

Hi ,

It seems that the new 4.3 update brought new signatures for the overlap functionality.

I’ve fixed this by using the delegate for the begin overlap signature (that’s outlined in PrimitiveComponent.h by the DECLARE_DYNAMIC_MULTICAST_DELEGATE_FiveParams macro, here) and should give you an Overlap function declaration that looks like:

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

If you’re ever going to use the end overlap, it uses a different signature, so use this function declaration instead:

void OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

This should fix your compiler errors :slight_smile:

EDIT: Also (for other people reading this), if your overlap does not work, make sure to add [YourGameName]_API to your class declaration, like so:

class YOURGAMENAME_API UYourComponent : public USphereComponent

Hey OrganicHumans,

I tried out your suggestion and it appears to have solved the problem. Thanks so much for your help!

Awesome, no problem! Glad I could be of help :slight_smile:

Worked for me too! I was using the LightSwitchCodeOnly code from this which doesn’t work in 4.3: CPP Only Example | Unreal Engine Documentation

Awesome Daveoh, glad to hear it helped :slight_smile:

It would be really great if you could post an example cpp code that actually works with this fix.

Since this is part of a tutorial problem its hard to figure out how to implement your fix in the code for an beginner like myself.

It would be so helpful to me and maybe other beginners to get this tutorial actually fixed
Thanks.

I agree with Spaehling

But yet, let me get this straight. So you’re saying that the OnComponentBeginOverlap stuff is replaced with a function called OnBeginOverlap as well as OnEndOverlap???

Answering my own commented question. I understand completely… basicaly it’s saying for the AddDynamic overlap to work, it has to have 5 paramters.

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

because it used to be like this

void OnBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

but it’s like the one on top instead… thanks for everthing :slight_smile:

Thanks, worked for me too! This is my header file code:

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once

#include "GameFramework/Actor.h"
#include "LightSwitchCodeOnly.generated.h"

/**
 * 
 */
UCLASS()
class THIRDPERSONLIGHT442_API ALightSwitchCodeOnly : public AActor
{
	GENERATED_UCLASS_BODY()

	/** point light component */
	UPROPERTY(VisibleAnywhere, Category = "Switch Components")
	TSubobjectPtr<class UPointLightComponent> PointLight1;

	/** sphere component */
	UPROPERTY(VisibleAnywhere, Category = "Switch Components")
		TSubobjectPtr<class USphereComponent> Sphere1;

	/** called when something overlaps the sphere component */
	UFUNCTION()
		void OnBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
	UFUNCTION()
		void OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
	/** the desired intensity for the light */
	UPROPERTY(VisibleAnywhere, Category = "Switch Variables")
		float DesiredIntensity;
};

And cpp file:
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include “thirdpersonlight442.h”
#include “LightSwitchCodeOnly.h”

ALightSwitchCodeOnly::ALightSwitchCodeOnly(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	DesiredIntensity = 3000.0f;

	PointLight1 = PCIP.CreateDefaultSubobject<UPointLightComponent>(this, "PointLight1");
	PointLight1->Intensity = DesiredIntensity;
	PointLight1->bVisible = true;
	RootComponent = PointLight1;

	Sphere1 = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("Sphere1"));
	Sphere1->InitSphereRadius(250.0f);
	Sphere1->AttachParent = RootComponent;

	Sphere1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnBeginOverlap);
	    // set up a notification for when this component overlaps something
	Sphere1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnEndOverlap);
}


void ALightSwitchCodeOnly::OnBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		PointLight1->ToggleVisibility();
	}
}

void ALightSwitchCodeOnly::OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		PointLight1->ToggleVisibility();
	}
}