"AddDynamic" not working

Hi All users.
I Have a New Actor in “Unreal Engine 4.5.1” But This Actor Not Working.

Header File TriggerTest.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

/**
 * 
 */
UCLASS()
class ARMITA_API ATriggerTest : public AActor
{
	GENERATED_UCLASS_BODY()

	TSubobjectPtr<UBoxComponent>Box;
	TSubobjectPtr<UPointLightComponent>Light;

	UFUNCTION()
		void Debug(FString Massage);
	UFUNCTION()
		void OnBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult);
	UFUNCTION()
		void OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult);
};

C++ File TriggerTest.cpp

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

#include "Armita.h"
#include "TriggerTest.h"


ATriggerTest::ATriggerTest(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	Box = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Box"));
	Light = PCIP.CreateDefaultSubobject<UPointLightComponent>(this, TEXT("Light"));
	Box->bGenerateOverlapEvents = true;
	Box->SetRelativeScale3D(FVector(2,2,5));
	RootComponent = Box;
	Light->Intensity = 1000;
	Light->SetLightColor(FColor::Red);
	Light->AttachParent = RootComponent;
	Box->OnComponentBeginOverlap.AddDynamic(this, &ATriggerTest::OnBeginOverlap);
	Box->OnComponentEndOverlap.AddDynamic(this, &ATriggerTest::OnEndOverlap);
}
void ATriggerTest::OnBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult)
	{

		Light->SetLightColor(FColor::Green);
		Debug("Open");
	}
void ATriggerTest::OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult)
	{

		Light->SetLightColor(FColor::Red);
		Debug("Close");

	}
	void ATriggerTest::Debug(FString Massage)
	{
		/*if (UEngine)
		{
			UEngine::AddOnScreenDebugMassage(-1, 1, FColor::Red, Massage);


		}*/

	}



Unreal Engine 4.5.1 ERROR

I had no issue getting begin overlap to fire off in 4.4, however I can confirm that I’ve had trouble getting begin overlap to fire via dynamic binding in 4.5, and yes I made sure the binding function was UFUNCTION(), and I also tried putting add dynamic later on, post init or begin play, but I still could not get it to activate for two collision channels that were set to overlap with each other very specifically.

I was testing in a character class to detect overlaps with static mesh actor deriving powerups.

I was adding the binding to the static mesh actor deriving class to detect if a player had collided with itself.

This definitely merits further investigation!

If no answers come forth for a while I can make a repro case.

Rama

1 Like

ok .
my problem was Solved.

Header File :

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

#pragma once

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

/**
 * 
 */
UCLASS()
class ARMITA_API ATriggerTest : public AActor
{
	GENERATED_UCLASS_BODY()

	TSubobjectPtr<UBoxComponent>Box;
	TSubobjectPtr<UPointLightComponent>Light;

	UFUNCTION()
		void Debug(FString Massage);
	UFUNCTION()
		void OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult);
	UFUNCTION()
		void OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};

Cpp File :

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

#include "Armita.h"
#include "TriggerTest.h"


ATriggerTest::ATriggerTest(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	Box = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Box"));
	Light = PCIP.CreateDefaultSubobject<UPointLightComponent>(this, TEXT("Light"));
	Box->bGenerateOverlapEvents = true;
	Box->SetRelativeScale3D(FVector(2,2,5));
	RootComponent = Box;
	Light->Intensity = 10000;
	Light->SetLightColor(FColor::Red);
	Light->AttachParent = RootComponent;
	Box->OnComponentBeginOverlap.AddDynamic(this, &ATriggerTest::OnOverlap);
	Box->OnComponentEndOverlap.AddDynamic(this, &ATriggerTest::OnEndOverlap);
}
void ATriggerTest::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult)
	{

		Light->SetLightColor(FColor::Green);
		Debug("Open");
	}
void ATriggerTest::OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
	{

		Light->SetLightColor(FColor::Red);
		Debug("Close");

	}
	void ATriggerTest::Debug(FString Massage)
	{
		/*if (UEngine)
		{
			UEngine::AddOnScreenDebugMassage(-1, 1, FColor::Red, Massage);


		}*/

	}

Thanks To all.

1 Like

Just came across this issue in my own learning. Wanted to add/second that the UFUNCTION was integral for my AddDynamic call to work.

To anyone seeing this recently: Adding UFUNCTION() will make your dynamic events work.

It also appears that you need to add the callback in your BeginPlay() method too (4.18.3). If added in the constructor for your AActor subclass it doesn’t seem to work.

I just had the same issue and as Craimasjien said: you better need add the AddDynamic(…) line in BeginPlay(). Thats why: if you have an existing Character Blueprint and decide to Add some OverlapEventhandling functionality to it, UE4 doesn´t register this call in the Constructor (but if you FIRST use AddDynamic in the constructor and THEN make the Character_BP out of it it works). Really annoying because the compiler does no error printing and you search for hours to find the bug.

1 Like

Thanks, this works fine!!!

Thanks SO much for posting this. I’ve also been searching for hours for a solution. I’m using 4.21 and could not get AddDynamic(,) to fire either. Adding the statement to BeginPlay() did the trick.

You don’t actually have to AddDynamic in BeginPlay. If it doesn’t work in the constructor just delete the asset from the world, then add it again to call its constructor.

That’s true, but why do I have to add UFUNCTION to bind a function reference to a delegate?
And why don’t they mention it in docs

Programming in Unreal/C++ sometimes feels like someone deliberately set up traps for you.

That “UFUNCTION” thing is something you come across at some point, be it here in the forums or in some other blog, but not in the docs.

To me, the worst part is that until I have figured out how something works, I never know whether the problem is my stupidity or just some quirk in the Engine alongside a lack of documentation.

  • Here I have strong suspicions that the player start mechanism just doesn’t work.

  • Here checking the boxes for “replicates” and “net load on client” in a somewhat contradictory manner solves a problem that (probably) shouldn’t be there to begin with.

  • And here I just fell into a beginner trap because I didn’t know that the replication mechanism always ever only serves to let data flow from the server to the client. (For the other direction you need a Server RPC)

Epic Games basically provides me Unreal Engine for free (unless I make more than a million USD with my game). … which is especially crazy given that UE is the most technologically advanced engine of them all. Along with the Epic MegaGrants, their support for game development is huge.

I guess a big reason why developping with UE isn’t “fun” is that UE wasn’t initially thought to be a community project. Now that the uses of UE are extended way beyond 3D-Shooters, it appears that Unreal Engine not only lacks documentation but also clean interfaces.

But imagine you would sit down, clear Unreal Engine of all the weirdness and provide new, more elegant abstractions (with full support for C++ 20) … you would have a lot of work (if such project can be finished at all) and you would totally break the engine for all older versions, leaving the veterans confused. All of this without a strict guarantee that the new product is any better.

Meanwhile, you realize that C++ isn’t even a good choice as a programming language. It’s just there because it always has been.

So there we are, waiting for the rust community to save us, if they ever succeed in their ambitious quest.

(Until then, yes, updating the docs, adding written tutorials and guides - no more YouTube, thanks - and having staff answer here more frequently would be great)

I suppose everyone comes across this :smiley:

You have syntax error.
true:
void OnBeginOverlap
(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

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

You’ve forgot to write (UPrimitiveComponent* OverlappedComponent) at first.