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

English original Address

Follow up with a tutorial where an event is triggered when a character overlaps with a Trigger Box.

Create a new C++ TriggerBox subclass (which inherits from the TriggerBase class, which in turn inherits from the Actor class) and name it MyTriggerBox. Add the OnOverlapBegin and OnOverlapEnd functions to the header file.

 

Here is the final.h header file (the original header code was wrong, fixed).

#pragma once

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

/**
 *
 */
UCLASS()
class HELLOUNREAL_API AMyTriggerBox : public ATriggerBox {
    GENERATED_BODY()

  protected:

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

  public:

    // constructor sets default values for this actor's properties
    AMyTriggerBox();

    // declare overlap begin function
    UFUNCTION()
    void OnOverlapBegin(AActor* OverlappedActor, AActor* OtherActor);

    // declare overlap end function
    UFUNCTION()
    void OnOverlapEnd(AActor* OverlappedActor, AActor* OtherActor);
};
Copy the code

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

#include "MyTriggerBox.h"
// include draw debu 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 shall send OnActorBeginOverlap. AddDynamic and OnActorEndOverlap AddDynamic registered overlapping events.

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

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

void AMyTriggerBox::BeginPlay()
{
	Super::BeginPlay();

    DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Purple, true, -1, 0, 5);
	
}
Copy the code

Next, we’ll write an overlap function that prints a message to the screen indicating the actor that enters and exits the trigger box.

void AMyTriggerBox::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) { // check if Actors do not equal nullptr and that if (OtherActor && (OtherActor ! = this)) { // print to screen using above defined method when actor enters trigger box print("Overlap Begin"); printFString("Overlapped Actor = %s", *OverlappedActor->GetName()); } } void AMyTriggerBox::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) { if (OtherActor && (OtherActor ! = this)) { // print to screen using above defined method when actor leaves trigger box print("Overlap Ended"); printFString("%s has left the Trigger Box", *OtherActor->GetName()); }}Copy the code

Compile the code and drag and drop the new actor into the game as shown below

The box that runs looks like this

 

Here is the final.cpp file.

#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)) #include "MyTriggerBox.h" // include draw debu helpers header file #include "DrawDebugHelpers.h" AMyTriggerBox::AMyTriggerBox() { //Register Events OnActorBeginOverlap.AddDynamic(this, &AMyTriggerBox::OnOverlapBegin); OnActorEndOverlap.AddDynamic(this, &AMyTriggerBox::OnOverlapEnd); } // Called when the game starts or when spawned void AMyTriggerBox::BeginPlay() { Super::BeginPlay(); DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Purple, true, -1, 0, 5); } void AMyTriggerBox::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) { // check if Actors do not equal nullptr and that if (OtherActor && (OtherActor ! = this)) { // print to screen using above defined method when actor enters trigger box print("Overlap Begin"); printFString("Overlapped Actor = %s", *OverlappedActor->GetName()); } } void AMyTriggerBox::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) { if (OtherActor && (OtherActor ! = this)) { // print to screen using above defined method when actor leaves trigger box print("Overlap Ended"); printFString("%s has left the Trigger Box", *OtherActor->GetName()); }}Copy the code

Finally, press the Play button to move the player in and out of the trigger box, resulting in the final image shown below.