六一的部落格


关关难过关关过,前路漫漫亦灿灿。




概览

-
健康组件 ActorComponent 自定义,纯逻辑
文本组件 TextRenderComponent 显示生命值

创建健康组件


创建Component/STUHealthComponent

虚幻编辑器

  • ActorComponent


  • 公有类



调整健康组件框架

C++

ShootThemUp: Components/STUHealthComponent.h

ShootThemUp: Components/STUHealthComponent.cpp

-
默认构造函数 将每帧调用TickComponent函数标志置为false
BeginPlay
TickComponent 屏蔽,使用基类实现

实现


添加生命值上限

protected

ShootThemUp: Components/STUHealthComponent.h

1UPROPERTY(EditDefaultsOnly, meta = (ClampMin = "0.0", ClampMax = "1000.0"))
2float MaxHealth = 100.0f;	

添加生命值

private

ShootThemUp: Components/STUHealthComponent.h

1float Health = 0.0f;

初始化生命值

ShootThemUp: Components/STUHealthComponent.cpp

1// BeginPlay
2Health = MaxHealth;

添加接口,返回生命值

public

ShootThemUp: Components/STUHealthComponent.h

1UFUNCTION(BlueprintCallable)
2float GetHealth() const { return Health; }

为游戏角色添加健康组件

C++

  1. 添加STUHealthComponent类型成员

    ShootThemUp: Player/STUBaseCharacter.h
    1class USTUHealthComponent;
    2
    3// protected
    4UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
    5USTUHealthComponent *HealthComponent;
  2. 初始化组件: STUHealthComponent无表示,不用设置上级组件

    ShootThemUp: Player/STUBaseCharacter.cpp
    1#include "Components/STUHealthComponent.h"
    2
    3// 构造函数
    4HealthComponent = CreateDefaultSubobject<USTUHealthComponent>("HealthComponent");

为游戏角色添加文本组件

C++

  1. 添加UTextRenderComponent类型成员

    ShootThemUp: Player/STUBaseCharacter.h
    1class UTextRenderComponent;
    2
    3// protected
    4UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
    5UTextRenderComponent *HealthTextComponent;	
    
  2. 初始化组件

    ShootThemUp: Player/STUBaseCharacter.cpp
    1#include "Components/TextRenderComponent.h"
    2
    3// 构造函数
    4HealthTextComponent = CreateDefaultSubobject<UTextRenderComponent>("HealthTextComponent");
    5HealthTextComponent->SetupAttachment(GetRootComponent());
  3. 每帧更新HealthTextComponent文本

    ShootThemUp: Player/STUBaseCharacter.cpp
    1// Tick
    2HealthTextComponent->SetText(FText::FromString(FString::Printf(TEXT("%.0f"), HealthComponent->GetHealth())));

检查组件是否成功初始化

使用IDE编译时,若运行游戏崩溃,可在IDE中查看断点

ShootThemUp: Player/STUBaseCharacter.cpp

1// BeginPlay
2check(HealthComponent);
3check(HealthTextComponent);

调整文本组件

虚幻编辑器

BP_STUBaseCharacter


查看组件


HealthComponent和CharacterMovement无层级:二者均无表示,纯逻辑;创建后无需挂在其他组件下面


查看HealthComponent



调整TextRender组件


-
Tranform Z: 100 Yaw: 180 第三视角:运行时摄像机对着 Character 背面
Text 0 默认值
Horizontal Alignment Center 水平对齐
Text Render Color 深蓝色 文本颜色



效果图


实时显示游戏角色生命值



概览

-
健康组件 ActorComponent 自定义,纯逻辑
文本组件 TextRenderComponent 显示生命值

创建健康组件


创建Component/STUHealthComponent

虚幻编辑器

  • ActorComponent


  • 公有类



调整健康组件框架

C++

ShootThemUp: Components/STUHealthComponent.h

ShootThemUp: Components/STUHealthComponent.cpp

-
默认构造函数 将每帧调用TickComponent函数标志置为false
BeginPlay
TickComponent 屏蔽,使用基类实现

实现


添加生命值上限

protected

ShootThemUp: Components/STUHealthComponent.h

1UPROPERTY(EditDefaultsOnly, meta = (ClampMin = "0.0", ClampMax = "1000.0"))
2float MaxHealth = 100.0f;	

添加生命值

private

ShootThemUp: Components/STUHealthComponent.h

1float Health = 0.0f;

初始化生命值

ShootThemUp: Components/STUHealthComponent.cpp

1// BeginPlay
2Health = MaxHealth;

添加接口,返回生命值

public

ShootThemUp: Components/STUHealthComponent.h

1UFUNCTION(BlueprintCallable)
2float GetHealth() const { return Health; }

为游戏角色添加健康组件

C++

  1. 添加STUHealthComponent类型成员

    ShootThemUp: Player/STUBaseCharacter.h
    1class USTUHealthComponent;
    2
    3// protected
    4UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
    5USTUHealthComponent *HealthComponent;
  2. 初始化组件: STUHealthComponent无表示,不用设置上级组件

    ShootThemUp: Player/STUBaseCharacter.cpp
    1#include "Components/STUHealthComponent.h"
    2
    3// 构造函数
    4HealthComponent = CreateDefaultSubobject<USTUHealthComponent>("HealthComponent");

为游戏角色添加文本组件

C++

  1. 添加UTextRenderComponent类型成员

    ShootThemUp: Player/STUBaseCharacter.h
    1class UTextRenderComponent;
    2
    3// protected
    4UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
    5UTextRenderComponent *HealthTextComponent;	
    
  2. 初始化组件

    ShootThemUp: Player/STUBaseCharacter.cpp
    1#include "Components/TextRenderComponent.h"
    2
    3// 构造函数
    4HealthTextComponent = CreateDefaultSubobject<UTextRenderComponent>("HealthTextComponent");
    5HealthTextComponent->SetupAttachment(GetRootComponent());
  3. 每帧更新HealthTextComponent文本

    ShootThemUp: Player/STUBaseCharacter.cpp
    1// Tick
    2HealthTextComponent->SetText(FText::FromString(FString::Printf(TEXT("%.0f"), HealthComponent->GetHealth())));

检查组件是否成功初始化

使用IDE编译时,若运行游戏崩溃,可在IDE中查看断点

ShootThemUp: Player/STUBaseCharacter.cpp

1// BeginPlay
2check(HealthComponent);
3check(HealthTextComponent);

调整文本组件

虚幻编辑器

BP_STUBaseCharacter


查看组件


HealthComponent和CharacterMovement无层级:二者均无表示,纯逻辑;创建后无需挂在其他组件下面


查看HealthComponent



调整TextRender组件


-
Tranform Z: 100 Yaw: 180 第三视角:运行时摄像机对着 Character 背面
Text 0 默认值
Horizontal Alignment Center 水平对齐
Text Render Color 深蓝色 文本颜色



效果图