最简单的UE4C++教程——触发器框【⼆⼗】
原教程是基于 UE 4.18,我是基于 UE 4.25】
接上⼀节教程,在本教程中,当⾓⾊与⼀个触发器框(Trigger Box)发⽣重叠时将触发⼀个事件。
创建⼀个新的 C++ TriggerBox ⼦类【它继承⾃ TriggerBa 类,⽽它⼜继承⾃ Actor 类】并将其命名为 MyTriggerBox 。在头⽂件中添加 OnOverlapBegin 和 OnOverlapEnd 函数。
下⾯是最终的 .h 头⽂件【原教程的头⽂件代码错了,已修复】。
#pragma once
#include "CoreMinimal.h"
#include "Engine/TriggerBox.h"
#include "ated.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 ts 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);
};
在 .cpp ⽂件中,为了帮助我们可视化触发器框,我们必须 #include DrawDebugHelpers.h ⽂件。
#include "MyTriggerBox.h"
// include draw debu helpers header file
#include "DrawDebugHelpers.h"
我们也可以 #define ⼀些调试⽇志的快捷⽅式。
#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))在该 actor 的构造函数中,我们将向 OnActorBeginOverlap.AddDynamic 和 OnActorEndOverlap.AddDynamic 注册重叠事件。
AMyTriggerBox::AMyTriggerBox()
{
主题公园//Register Events
OnActorBeginOverlap.AddDynamic(this, &AMyTriggerBox::OnOverlapBegin);
OnActorEndOverlap.AddDynamic(this, &AMyTriggerBox::OnOverlapEnd);
}
在 BeginPlay 上,我们将使⽤ DrawDebugBox 绘制调试框。
void AMyTriggerBox::BeginPlay()
{
Super::BeginPlay();
DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Purple, true, -1, 0, 5);
}
接下来,我们将编写重叠函数,它将向屏幕打印⼀条消息,指⽰进⼊和退出触发器框的 actor 。
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());
}
医德医风工作总结
}
编译代码,将新 actor 拖放到游戏中,如下所⽰
运⾏起来的框是这样的
下⾯是最后的 .cpp ⽂件。
#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());
}
}
最后,按下播放键,操控玩家在触发器框⾥进进出出,得到的最终效果图如下所⽰。