The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

The next tutorial will trigger overlapping events with specific actors.

Create a new C++ TriggerBox class and name it TriggerBoxSpecificActor. In the header file, we will declare two void overlap functions and AActor classes for that particular actor.

Here is the final header code.

TriggerBoxSpecificActor.h

#pragma once

#include "CoreMinimal.h"
#include "Engine/TriggerBox.h"
#include "TriggerBoxSpecificActor.generated.h"


UCLASS(a)class UNREALCPP_API ATriggerBoxSpecificActor : public ATriggerBox
{
	GENERATED_BODY(a)protected:

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

	// constructor sets default values for this actor's properties
	ATriggerBoxSpecificActor(a);// overlap begin function
	UFUNCTION(a)void OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor);

	// overlap end function
	UFUNCTION(a)void OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor);

	// specific actor for overlap
	UPROPERTY(EditAnywhere)
	class AActor* SpecificActor;
	
};
Copy the code

In the.cpp file, to help us visualize the trigger box, we must use the #include drawdebughelpers.h file.

#include "TriggerBoxSpecificActor.h"
// include draw debug helpers header file
#include "DrawDebugHelpers.h"
Copy the code

We can also #define some debug log shortcuts.

#define print(text) if(GEngine) GEngine - > AddOnScreenDebugMessage (1, 1.5, FColor: : Green, text)
#define printFString(text, fstring) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT(text), fstring))
Copy the code

In the constructor of the actor, we will to OnActorBeginOverlap. AddDynamic and OnActorEndOverlap AddDynamic registered overlapping events.

ATriggerBoxSpecificActor::ATriggerBoxSpecificActor()
{
    //Register Events
    OnActorBeginOverlap.AddDynamic(this, &ATriggerBoxSpecificActor::OnOverlapBegin);
    OnActorEndOverlap.AddDynamic(this, &ATriggerBoxSpecificActor::OnOverlapEnd);
}
Copy the code

On BeginPlay, we will use the DrawDebugBox to draw the debug box.

// Called when the game starts or when spawned
void ATriggerBoxSpecificActor::BeginPlay(a)
{
	Super::BeginPlay(a);DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Green, true.- 1.0.5);
	
}
Copy the code

Next, we’ll write an overlapping function that prints a message to the screen when a particular actor enters and exits a TriggerBox. In the overlapping function, we check whether the actors in TriggerBox are our specific actors by OtherActor == SpecificActor.

void ATriggerBoxSpecificActor::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor)
{
    //if the overlapping actor is the specific actor we identified in the editor
    if(OtherActor && (OtherActor ! =this) && OtherActor == SpecificActor )
    {
        print("Overlap Begin");
        printFString("Overlapping Actor = %s", *OtherActor->GetName()); }}void ATriggerBoxSpecificActor::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor)
{
    //if the overlapping actor is the specific actor we identified in the editor
    if(OtherActor && (OtherActor ! =this) && OtherActor == SpecificActor )
    {
        print("Overlap End");
        printFString("%s has left the Trigger Box", *OtherActor->GetName()); }}Copy the code

Compile the code. Drag and drop the new actor into the game.

Here’s how it works

 

Add an actor, a specific actor, to the Actor details panel.

 

Click the Play button to move specific actors in and out of the TriggerBox to trigger overlapping events.

Here’s how it looks in and out