虚幻4C++学习文档翻译4 下载本文

内容发布更新时间 : 2024/7/3 6:18:49星期一 下面是文章的全部内容请认真阅读。

创建和NPC的对话窗体

我们需要创建一个继承自HUD的类

UCLASS()

class CPLUSLEARN_API AMyHUDMessage : public AHUD {

public : };

void AMyHUDMessage::DrawHUD() {

DrawText(\欢迎来到虚幻的世界!!\, FVector2D(0, 0), hudFont, FVector2D(1, 1), DrawLine(200, 300, 400, 500, FLinearColor::Blue); Super::DrawHUD();

virtual void DrawHUD() override;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont) UFont *hudFont; GENERATED_BODY()

FColor::White); }

我们在自己的HUD的类中定义了一个字体,并且在界面上绘出一个线框和文字

重新编译项目,我们进入UE4中,创建一个继承自MyHUD的蓝图类,同时设置我们自己的字体信息。最后在我们的游戏模式中将默认的HUD改成我们自己创建的HUD

下面我们要用一个一个列表来存放这些对话的信息,我们打开自己创建的C++的HUD类

void AMyHUDMessage::DrawHUD() {

//DrawLine(200, 300, 400, 500, FLinearColor::Blue); Super::DrawHUD();

}

void AMyHUDMessage::addMessage(Message msg) { }

messages.Add(msg); }

//当该条消息的时间小于0的时候将该条信息移除 if (messages[c].time < 0) { }

messages.RemoveAt(c);

//时间逐步缩减

messages[c].time -=GetWorld()->GetDeltaSeconds();

//使用字体绘制出我们的信息(信息,颜色,宽度+边距,高度+边距,字体)

DrawText(messages[c].message, messages[c].color, x + pad, y + pad, hudFont);

//绘制出背景

DrawRect(FLinearColor::Black, x, y, Canvas->SizeX, messageH);

float messageH = outputHeight + 2.f*pad;//信息的高度就是输出高度+2倍的间隔 float x = 0.f, y = c*messageH;

for (int c = messages.Num() - 1; c >= 0; c--) {

float outputWidth, outputHeight, pad = 10.f; //定义输出的宽度高度和间隔

//获取出信息内容大小

GetTextSize(messages[c].message, outputWidth, outputHeight, hudFont, 1.f); //DrawText(\

下面我们必须要完成我们的NPC实体

UCLASS()

class CPLUSLEARN_API ANonePlayerEntity : public ACharacter

{ public: };

//NPC所需要传递的话

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)

FString NPCMessage; //碰撞器触发器

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)

TSubobjectPtr ProxSphere; //碰撞事件

UFUNCTION(BlueprintNativeEvent, Category = \)

void Prox(AActor* otherActor, UPrimitiveComponent* OtherComp, int32 // Called to bind functionality to input

virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) // Called when the game starts or when spawned virtual void BeginPlay() override;

// Called every frame

virtual void Tick( float DeltaSeconds ) override; // Sets default values for this character's properties ANonePlayerEntity(); GENERATED_UCLASS_BODY()

override;

OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

这个是我们自己的NPC类的头文件,我们新添加了碰撞检测事件,里面的参数都是固定的API的参数,指定是Collision类别的碰撞器才会触发这个碰撞事件

ANonePlayerEntity::ANonePlayerEntity(const class FPostConstructInitializeProperties &PCIP) :Super(PCIP) {

// Set this character to call Tick() every frame. You can turn this off to improve PrimaryActorTick.bCanEverTick = true; performance if you don't need it.

}

// Called when the game starts or when spawned void ANonePlayerEntity::BeginPlay() { }

// Called every frame

void ANonePlayerEntity::Tick( float DeltaTime ) { }

// Called to bind functionality to input

void ANonePlayerEntity::SetupPlayerInputComponent(class UInputComponent* InputComponent) { }

void ANonePlayerEntity::Prox_Implementation(AActor * otherActor, UPrimitiveComponent* othrComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) {

//将其他玩家进入该区域转化为avatar,如果是空物体的,那么直接返回 if (Cast(otherActor) == nullptr) {

return;

//碰撞发生事情,当有交互时

Super::SetupPlayerInputComponent(InputComponent); Super::Tick( DeltaTime ); Super::BeginPlay();

ProxSphere = PCIP.CreateDefaultSubobject(this, TEXT(\//放置在根节点下面

ProxSphere->AttachTo(RootComponent); //设置碰撞半径

ProxSphere->SetSphereRadius(32.f); //当有人进去碰撞区域

ProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANonePlayerEntity::Prox); NPCMessage = \, i am Owner\;//设置默认的NOC对话 Sphere\));