六一的部落格


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




概览

  • 创建武器组件类 STUWeaponComponent
  • 武器组件负责武器相关逻辑: 将SpawnWeapon移到武器组件
  • 为游戏角色添加武器组件
  • 为武器类添加 Fire 接口, 对应扣动扳机逻辑
  • 为游戏角色绑定鼠标左键, 武器组件提供回调函数 Fire , 调用武器类 Fire 接口

初始化武器组件类


创建C++类: Components/STUWeaponComponent

虚幻编辑器

-
ActorComponent
Public
Components

调整框架

C++

-
构造函数 每帧调用置false
TickComponent 屏蔽

ShootThemUp: Components/STUWeaponComponent.h

ShootThemUp: Components/STUWeaponComponent.cpp


将SpawnWeapon逻辑移动到武器组件

C++


在STUWeaponComponent中实现SpawnWeapon

  • 添加武器类型属性, 动态创建Actor时使用

    1. ShootThemUp: Components/STUWeaponComponent.h

      protected
      1class ASTUBaseWeapon;
      2
      3UPROPERTY(EditDefaultsOnly)
      4TSubclassOf<ASTUBaseWeapon> WeaponClass;
    2. ShootThemUp: Components/STUWeaponComponent.cpp
      1#include "Weapon/STUBaseWeapon.h"
  • 添加变量: 存放挂载点名称

    protected

    ShootThemUp: Components/STUWeaponComponent.h

    1UPROPERTY(EditDefaultsOnly)
    2FName WeaponAttachPointName = "WeaponSocket";
  • 添加变量: 存放武器指针

    private

    使用UPROPERTY宏标识, 使得Garbage Collector管理该指针指向的内存

    1UPROPERTY()
    2ASTUBaseWeapon *CurrentWeapon = nullptr;
  • 添加函数: SpawnWeapon

    1. ShootThemUp: Components/STUWeaponComponent.h

      private
      1void SpawnWeapon();
    2. ShootThemUp: Components/STUWeaponComponent.cpp

      处理不正确则退出: 统一结构, 减少缩进
       1#include "Engine/World.h"
       2#include "Components/SkeletalMeshComponent.h"
       3#include "Weapon/STUBaseWeapon.h"
       4#include "GameFramework/Character.h"
       5
       6void USTUWeaponComponent::SpawnWeapon()
       7{
       8    if (!GetWorld()) return;
       9
      10    ACharacter *Character = Cast<ACharacter>(GetOwner());
      11    if (!Character) return;
      12
      13    CurrentWeapon = GetWorld()->SpawnActor<ASTUBaseWeapon>(WeaponClass);
      14    if (!CurrentWeapon) return;
      15
      16    FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, false);
      17    CurrentWeapon->AttachToComponent(Character->GetMesh(), AttachmentRules, WeaponAttachPointName);
      18}
    3. 武器组件出现在场景中时, 创建并装载武器: 在BeginPlay中调用SpawnWeapon

移除STUBaseCharacter中的SpawnWeapon

  1. ShootThemUp: Player/STUBaseCharacter.h
    • STUBaseWeapon前向声明
    • WeaponClass成员
    • SpawnWeapon声明
  2. ShootThemUp: Player/STUBaseCharacter.cpp
    • 头文件: STUBaseWeapon, World, SkeletalMeshComponent
    • BeginPlay中调用SpawnWeapon
    • SpawnWeapon

为游戏角色添加武器组件

C++

武器组件是纯逻辑组件, 不用设置上级组件

  1. 添加组件

    protected

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

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

查看武器绑定

虚幻编辑器

  1. 为武器组件设置武器类型

    BP_STUBaseCharacter > Weapon Component > Weapon Class: 设置为BP_STUBaseWeapon

  2. 效果图: 和之前效果一致



实现游戏角色扣动扳机


为游戏角色绑定键位: 点击鼠标左键触发扣动扳机

项目设置 > Engine > Input > Bindings > Action Mappings

-
函数描述 Fire
键位 Left Mouse Button



武器组件提供回调函数: 调用武器Fire接口

C++


为武器类添加Fire接口

具体类型武器派生自STUBaseWeapon, 各自对Fire的实现不同. 将Fire定义为虚函数, 打印日志

  1. ShootThemUp: Weapon/STUBaseWeapon.h

    public
    1virtual void Fire();
  2. ShootThemUp: Weapon/STUBaseWeapon.cpp
    1DEFINE_LOG_CATEGORY_STATIC(LogBaseWeapon, All, All);
    2
    3void ASTUBaseWeapon::Fire()
    4{
    5    UE_LOG(LogBaseWeapon, Display, TEXT("Fire!"));
    6}

回调函数

  1. ShootThemUp: Components/STUWeaponComponent.h

    public
    1void Fire();            
    
  2. ShootThemUp: Components/STUWeaponComponent.cpp
    1void USTUWeaponComponent::Fire()
    2{
    3    if (!CurrentWeapon) return;
    4    CurrentWeapon->Fire();
    5}

绑定绑定函数描述和回调函数

ShootThemUp: Player/STUBaseCharacter.cpp

  1. 检查组件初始化
    1// BeginPlay
    2check(WeaponComponent);           
    
  2. 绑定
    1// SetupPlayerInputComponent
    2PlayerInputComponent->BindAction("Fire", IE_Pressed, WeaponComponent, &USTUWeaponComponent::Fire);

查看

点击鼠标左键, 输出日志


武器组件



概览

  • 创建武器组件类 STUWeaponComponent
  • 武器组件负责武器相关逻辑: 将SpawnWeapon移到武器组件
  • 为游戏角色添加武器组件
  • 为武器类添加 Fire 接口, 对应扣动扳机逻辑
  • 为游戏角色绑定鼠标左键, 武器组件提供回调函数 Fire , 调用武器类 Fire 接口

初始化武器组件类


创建C++类: Components/STUWeaponComponent

虚幻编辑器

-
ActorComponent
Public
Components

调整框架

C++

-
构造函数 每帧调用置false
TickComponent 屏蔽

ShootThemUp: Components/STUWeaponComponent.h

ShootThemUp: Components/STUWeaponComponent.cpp


将SpawnWeapon逻辑移动到武器组件

C++


在STUWeaponComponent中实现SpawnWeapon

  • 添加武器类型属性, 动态创建Actor时使用

    1. ShootThemUp: Components/STUWeaponComponent.h

      protected
      1class ASTUBaseWeapon;
      2
      3UPROPERTY(EditDefaultsOnly)
      4TSubclassOf<ASTUBaseWeapon> WeaponClass;
    2. ShootThemUp: Components/STUWeaponComponent.cpp
      1#include "Weapon/STUBaseWeapon.h"
  • 添加变量: 存放挂载点名称

    protected

    ShootThemUp: Components/STUWeaponComponent.h

    1UPROPERTY(EditDefaultsOnly)
    2FName WeaponAttachPointName = "WeaponSocket";
  • 添加变量: 存放武器指针

    private

    使用UPROPERTY宏标识, 使得Garbage Collector管理该指针指向的内存

    1UPROPERTY()
    2ASTUBaseWeapon *CurrentWeapon = nullptr;
  • 添加函数: SpawnWeapon

    1. ShootThemUp: Components/STUWeaponComponent.h

      private
      1void SpawnWeapon();
    2. ShootThemUp: Components/STUWeaponComponent.cpp

      处理不正确则退出: 统一结构, 减少缩进
       1#include "Engine/World.h"
       2#include "Components/SkeletalMeshComponent.h"
       3#include "Weapon/STUBaseWeapon.h"
       4#include "GameFramework/Character.h"
       5
       6void USTUWeaponComponent::SpawnWeapon()
       7{
       8    if (!GetWorld()) return;
       9
      10    ACharacter *Character = Cast<ACharacter>(GetOwner());
      11    if (!Character) return;
      12
      13    CurrentWeapon = GetWorld()->SpawnActor<ASTUBaseWeapon>(WeaponClass);
      14    if (!CurrentWeapon) return;
      15
      16    FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, false);
      17    CurrentWeapon->AttachToComponent(Character->GetMesh(), AttachmentRules, WeaponAttachPointName);
      18}
    3. 武器组件出现在场景中时, 创建并装载武器: 在BeginPlay中调用SpawnWeapon

移除STUBaseCharacter中的SpawnWeapon

  1. ShootThemUp: Player/STUBaseCharacter.h
    • STUBaseWeapon前向声明
    • WeaponClass成员
    • SpawnWeapon声明
  2. ShootThemUp: Player/STUBaseCharacter.cpp
    • 头文件: STUBaseWeapon, World, SkeletalMeshComponent
    • BeginPlay中调用SpawnWeapon
    • SpawnWeapon

为游戏角色添加武器组件

C++

武器组件是纯逻辑组件, 不用设置上级组件

  1. 添加组件

    protected

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

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

查看武器绑定

虚幻编辑器

  1. 为武器组件设置武器类型

    BP_STUBaseCharacter > Weapon Component > Weapon Class: 设置为BP_STUBaseWeapon

  2. 效果图: 和之前效果一致



实现游戏角色扣动扳机


为游戏角色绑定键位: 点击鼠标左键触发扣动扳机

项目设置 > Engine > Input > Bindings > Action Mappings

-
函数描述 Fire
键位 Left Mouse Button



武器组件提供回调函数: 调用武器Fire接口

C++


为武器类添加Fire接口

具体类型武器派生自STUBaseWeapon, 各自对Fire的实现不同. 将Fire定义为虚函数, 打印日志

  1. ShootThemUp: Weapon/STUBaseWeapon.h

    public
    1virtual void Fire();
  2. ShootThemUp: Weapon/STUBaseWeapon.cpp
    1DEFINE_LOG_CATEGORY_STATIC(LogBaseWeapon, All, All);
    2
    3void ASTUBaseWeapon::Fire()
    4{
    5    UE_LOG(LogBaseWeapon, Display, TEXT("Fire!"));
    6}

回调函数

  1. ShootThemUp: Components/STUWeaponComponent.h

    public
    1void Fire();            
    
  2. ShootThemUp: Components/STUWeaponComponent.cpp
    1void USTUWeaponComponent::Fire()
    2{
    3    if (!CurrentWeapon) return;
    4    CurrentWeapon->Fire();
    5}

绑定绑定函数描述和回调函数

ShootThemUp: Player/STUBaseCharacter.cpp

  1. 检查组件初始化
    1// BeginPlay
    2check(WeaponComponent);           
    
  2. 绑定
    1// SetupPlayerInputComponent
    2PlayerInputComponent->BindAction("Fire", IE_Pressed, WeaponComponent, &USTUWeaponComponent::Fire);

查看

点击鼠标左键, 输出日志