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

English original Address

In the next tutorial, an event is fired when actor and TriggerVolume overlap. TriggerBox is used in this tutorial, but the process should be similar.

Create a new C++ TriggerVolume subclass and name it MyTriggerVolume. Add the OnOverlapBegin and OnOverlapEnd functions to the header file.

Here is the final header file.

MyTriggerVolume.h

#pragma once

#include "CoreMinimal.h"
#include "Engine/TriggerVolume.h"
#include "MyTriggerVolume.generated.h"

/**
 * 
 */
UCLASS()
class UNREALCPP_API AMyTriggerVolume : public ATriggerVolume
{
	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
	AMyTriggerVolume();

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

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

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

#include "MyTriggerVolume.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.

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

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

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

	DrawDebugBox(GetWorld(), GetActorLocation(), GetActorScale()*100, FColor::Cyan, true, -1, 0, 5);
	
}
Copy the code

Next, we’ll write an overlapping function that prints a message to the screen indicating entry and exit of the TriggerVolume’s actor.

void AMyTriggerVolume::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) { if (OtherActor && (OtherActor ! = this)) { // print to screen using above defined method when actor enters trigger volume print("Overlap Begin"); printFString("Other Actor = %s", *OtherActor->GetName()); } } void AMyTriggerVolume::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) { if (OtherActor && (OtherActor ! = this)) { // print to screen using above defined method when actor leaves trigger volume print("Overlap Ended"); printFString("%s has left the Trigger Volume", *OtherActor->GetName()); }}Copy the code

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

 

Press the play button and move in and out of the trigger body.

Below is a rendering of the final run

 

 

\