How to detect triggerbox collision with actor

I need a way of checking if my player is colliding with a triggerbox in ue4

Something like this:

`if(player->isTouching(myTriggerBox)) { // do something }

void AMyBox::ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::White, TEXT(“Exit overlap”));
}
}
and header definition:
void ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);`

In your trigger box constructor specify the function you want called like this:

MyBox->OnComponentBeginOverlap.AddDynamic(this, &AMyTriggerActor::TriggerEnter);  
// I call it TriggerEnter, call it whatever  you like

and then have a function that does what you want to have happen:

void AMyTriggerActor::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
//do stuff
return;
}

Thanks so much man, I really appreciate it!

@pagancyc
I’ve tried using your advice, but when I try to call the adddynamic function I cannot find it, only Add, AddUnique and __Internal_AddDynamic. The class is of type actor, what am I doing wrong?

I’ve gotten help here, only fair that I pass it along.

If you upvote the answer and mark your question resolved, it will be helpful for the next person searching this topic.

And I will also get some sweet sweet internet karma! :wink:

That’s odd, AddDynamic doesn’t show up in my dropdown either, but it compiles and works. No idea what it doesn’t show up, kind of bizarre.

Here is a larger code dump, maybe it will help…

MyStuffDoer.h:

class STUFFDEMO_API AMyStuffDoer : public AActor
{
	GENERATED_BODY()
	
	AMyStuffDoer(const FObjectInitializer& ObjectInitializer);

	UShapeComponent* Box;

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

MyStuffDoer.cpp:

AMyStuffDoer::AMyStuffDoer(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
	Box = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Box"));
	Box->bGenerateOverlapEvents = true;
	Box->SetRelativeScale3D(Stretch);
	RootComponent = Box;
	Box->OnComponentBeginOverlap.AddDynamic(this, &AMyStuffDoer::TriggerEnter);
}

Did you declare it in your header file?

Well, then you’ve got me stumped. I’ve never seen any difference in declaration/syntax between the two other than ‘Begin’ and ‘End’.

@pagancyc
Thanks! Just one more thing, I tried adding a OnComponentEndOverlap dynamic like so:
Box->OnComponentEndOverlap.AddDynamic(this, &AMyBox::TriggerExit);
Doing so gave me the error no instance of function template matches the argument list. Any ideas?

@pagancyc Yes, I sure did

Heres my function definition:
void AMyBox::ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::White, TEXT(“Exit overlap”));
}
}
and header definition:
void ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

I could swear nothing is wrong, anyways let me know if you see whats wrong

I misspoke earlier, there is a difference besides Begin/End. The End function has different arguments:

void AMyStuffDoer::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
...
}