六一的部落格


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




在代码中添加检查

  • 使用 checkf , 和 check 相比, 可以设置提示信息
  • 使用 checkNoEntry , 等价于调用 check 返回false; 立即触发断言 assertion

STUBaseWeapon

ShootThemUp: Weapon/STUBaseWeapon.cpp

检查蓝图中设置的子弹数, 要求大于等于0

可以使用ClampMin和ClampMax进行元信息 meta information 检查

之后在蓝图设置错误数值触发检查

1// BeginPlay
2
3checkf(DefaultAmmo.Bullets > 0, TEXT("Bullets count couldn't be less or equal zero"));
4checkf(DefaultAmmo.Clips > 0, TEXT("Clips count couldn't be less or equal zero"));

STUWeaponComponent

ShootThemUp: Components/STUWeaponComponent.cpp

定义常量, 检查蓝图中配置的武器个数

1constexpr static int32 WeaponNum = 2;
2
3// BeginPlay
4checkf(WeaponData.Num() == 2, TEXT("Our character can hold only %d weapon items"), WeaponNum);

AnimNotify

ShootThemUp: Components/STUWeaponComponent.cpp

未在轨道中添加AnimNotify事件(找不到Notify)时, 输出日志, 并调用宏 checkNoEntry

 1// InitAnimations
 2
 3if (EquipFinishedNotify)
 4{
 5    // ...
 6}
 7else
 8{
 9    UE_LOG(LogWeaponComponent, Error, TEXT("Equip anim notify is forgetten to set"));
10    checkNoEntry();
11}
12
13if (ReloadFinishedNotify)
14{
15    // ...
16}
17else
18{
19    UE_LOG(LogWeaponComponent, Error, TEXT("Reload anim notify is forgetten to set"));
20    checkNoEntry();
21}

添加头文件AnimUtils.h

  • 在Public/Animations/添加AnimUtils.h
  • 纯辅助, 实用类, 供动画相关类使用

屏蔽FindNotifyByClass的定义和相关头文件

ShootThemUp: Components/STUWeaponComponent.h

1#include "Animation/AnimSequenceBase.h"

定义AnimUtils类

ShootThemUp: Animations/AnimUtils.h

声明为静态函数(不依赖具体类对象)

 1#pragma once
 2
 3#include "Animation/AnimSequenceBase.h"        
 4class AnimUtils
 5{
 6    public:
 7    template<typename T>
 8    static T* FindNotifyByClass(UAnimSequenceBase *Animation)
 9    {
10        if (!Animation) return nullptr;
11
12        const auto NotifyEvents = Animation->Notifies;
13        for (auto NotifyEvent : NotifyEvents)
14        {
15            auto AnimNotify = Cast<T>(NotifyEvent.Notify);
16            if (AnimNotify)
17            {
18                return AnimNotify;
19            }
20        }
21        return nullptr;
22    }
23};

使用

ShootThemUp: Components/STUWeaponComponent.cpp

  • 添加头文件
    1#include "Animations/AnimUtils.h"
  • 使用FindNotifyByClass时给出所属类名

添加头文件STUCoreTypes.h

  • 在Public/添加STUCoreTypes.h
  • 将别处定义的数据结构和委托类型整合, 标注使用的地方; 可以按WeaponTypes, HealthTypes进行分类
  • 修改后在头文件中包含STUCoreTypes.h

STUBaseWeapon

ShootThemUp: Weapon/STUBaseWeapon.h

  • FOnClipEmptySignature
  • FAmmoData

STUWeaponComponent

ShootThemUp: Components/STUWeaponComponent.h

  • FWeaponData
    1#include "Templates/SubclassOf.h"
    2class ASTUBaseWeapon;
    3class UAnimMontage;

STUHealthComponent

ShootThemUp: Components/STUHealthComponent.h

  • FOnDeathSignature
  • FOnHealthChangedSignature

为了让UHT正确生成数据结构、委托类型宏相关的所有代码, 添加头文件声明

格式 “filename.generated.h”

ShootThemUp: STUCoreTypes.h

1#include "STUCoreTypes.generated.h"

验证


将Content/Weapon/BP_STURifleWeapon > Default Ammo > Bullets设-1

=> 程序崩溃: 触发断言并输出日志


恢复


删除Content/Player/Animations/AM_Equip中的STUEquipFinishedAnimNotify

=> 程序崩溃: 触发checkNoEntry


恢复


打包游戏

  • 工具栏 > Platforms > Mac, 选择DebugGame, 可以看见日志
  • 工具栏 > Platforms > Mac > Package Project

打包



在代码中添加检查

  • 使用 checkf , 和 check 相比, 可以设置提示信息
  • 使用 checkNoEntry , 等价于调用 check 返回false; 立即触发断言 assertion

STUBaseWeapon

ShootThemUp: Weapon/STUBaseWeapon.cpp

检查蓝图中设置的子弹数, 要求大于等于0

可以使用ClampMin和ClampMax进行元信息 meta information 检查

之后在蓝图设置错误数值触发检查

1// BeginPlay
2
3checkf(DefaultAmmo.Bullets > 0, TEXT("Bullets count couldn't be less or equal zero"));
4checkf(DefaultAmmo.Clips > 0, TEXT("Clips count couldn't be less or equal zero"));

STUWeaponComponent

ShootThemUp: Components/STUWeaponComponent.cpp

定义常量, 检查蓝图中配置的武器个数

1constexpr static int32 WeaponNum = 2;
2
3// BeginPlay
4checkf(WeaponData.Num() == 2, TEXT("Our character can hold only %d weapon items"), WeaponNum);

AnimNotify

ShootThemUp: Components/STUWeaponComponent.cpp

未在轨道中添加AnimNotify事件(找不到Notify)时, 输出日志, 并调用宏 checkNoEntry

 1// InitAnimations
 2
 3if (EquipFinishedNotify)
 4{
 5    // ...
 6}
 7else
 8{
 9    UE_LOG(LogWeaponComponent, Error, TEXT("Equip anim notify is forgetten to set"));
10    checkNoEntry();
11}
12
13if (ReloadFinishedNotify)
14{
15    // ...
16}
17else
18{
19    UE_LOG(LogWeaponComponent, Error, TEXT("Reload anim notify is forgetten to set"));
20    checkNoEntry();
21}

添加头文件AnimUtils.h

  • 在Public/Animations/添加AnimUtils.h
  • 纯辅助, 实用类, 供动画相关类使用

屏蔽FindNotifyByClass的定义和相关头文件

ShootThemUp: Components/STUWeaponComponent.h

1#include "Animation/AnimSequenceBase.h"

定义AnimUtils类

ShootThemUp: Animations/AnimUtils.h

声明为静态函数(不依赖具体类对象)

 1#pragma once
 2
 3#include "Animation/AnimSequenceBase.h"        
 4class AnimUtils
 5{
 6    public:
 7    template<typename T>
 8    static T* FindNotifyByClass(UAnimSequenceBase *Animation)
 9    {
10        if (!Animation) return nullptr;
11
12        const auto NotifyEvents = Animation->Notifies;
13        for (auto NotifyEvent : NotifyEvents)
14        {
15            auto AnimNotify = Cast<T>(NotifyEvent.Notify);
16            if (AnimNotify)
17            {
18                return AnimNotify;
19            }
20        }
21        return nullptr;
22    }
23};

使用

ShootThemUp: Components/STUWeaponComponent.cpp

  • 添加头文件
    1#include "Animations/AnimUtils.h"
  • 使用FindNotifyByClass时给出所属类名

添加头文件STUCoreTypes.h

  • 在Public/添加STUCoreTypes.h
  • 将别处定义的数据结构和委托类型整合, 标注使用的地方; 可以按WeaponTypes, HealthTypes进行分类
  • 修改后在头文件中包含STUCoreTypes.h

STUBaseWeapon

ShootThemUp: Weapon/STUBaseWeapon.h

  • FOnClipEmptySignature
  • FAmmoData

STUWeaponComponent

ShootThemUp: Components/STUWeaponComponent.h

  • FWeaponData
    1#include "Templates/SubclassOf.h"
    2class ASTUBaseWeapon;
    3class UAnimMontage;

STUHealthComponent

ShootThemUp: Components/STUHealthComponent.h

  • FOnDeathSignature
  • FOnHealthChangedSignature

为了让UHT正确生成数据结构、委托类型宏相关的所有代码, 添加头文件声明

格式 “filename.generated.h”

ShootThemUp: STUCoreTypes.h

1#include "STUCoreTypes.generated.h"

验证


将Content/Weapon/BP_STURifleWeapon > Default Ammo > Bullets设-1

=> 程序崩溃: 触发断言并输出日志


恢复


删除Content/Player/Animations/AM_Equip中的STUEquipFinishedAnimNotify

=> 程序崩溃: 触发checkNoEntry


恢复


打包游戏

  • 工具栏 > Platforms > Mac, 选择DebugGame, 可以看见日志
  • 工具栏 > Platforms > Mac > Package Project