六一的部落格


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




说明

Behavior Tree Cleanup

NPC死亡后清除行为树


验证NPC死亡后, 行为树仍在运行


添加调试信息

  • 每次运行寻找敌人服务时, 输出日志

    ShootThemUp: AI/Services/STUFindEnemyService.cpp
    1// TickNode
    2UE_LOG(LogTemp, Display, TEXT("find enemy"));
  • 游戏角色死亡后输入日志

    ShootThemUp: Components/STUHealthComponent.cpp
    1// OnTakeAnyDamage
    2UE_LOG(LogTemp, Display, TEXT("character dead"));

设置NPC生命值

BP_STUAICharacter > Health Component > Details > Max Health

设置为5


查看

NPC死亡后, 行为树仍在运行: 直到Character的Destroy函数被调用



AIController::RunBehaviorTree

  1. UBehaviorTreeComponent派生自BrainComponent
  2. 调用UBrainComponent::StartTree实现

UBrainComponent::Cleanup

停止行为树运行


在AICharacter中覆写OnDeath


修改基类声明

  • 使之为虚函数
  • 移动到protected

    派生类可以覆写基类私有函数, 但无法调用基类实现

ShootThemUp: Player/STUBaseCharacter.h

1virtual void OnDeath();

在派生类覆写

protected

ShootThemUp: AI/STUAICharacter.h

1virtual void OnDeath() override;

ShootThemUp: AI/STUAICharacter.cpp

 1#include "BrainComponent.h"
 2
 3void ASTUAICharacter::OnDeath()
 4{
 5    Super::OnDeath();
 6
 7    const auto STUController = Cast<AAIController>(Controller);
 8    if (STUController && STUController->BrainComponent)
 9    {
10        STUController->BrainComponent->Cleanup();
11    }
12}

查看

  • 设置步枪伤害

    BP_STURifleWeapon > Details > Damage Amount , 设置为10



移除日志打印

ShootThemUp: AI/Services/STUFindEnemyService.cpp

ShootThemUp: Components/STUHealthComponent.cpp

ShootThemUp: Weapon/STUBaseWeapon.cpp

1// GetTraceData

ShootThemUp: Weapon/STULauncherWeapon.cpp

1// MakeShot

清除行为树



说明

Behavior Tree Cleanup

NPC死亡后清除行为树


验证NPC死亡后, 行为树仍在运行


添加调试信息

  • 每次运行寻找敌人服务时, 输出日志

    ShootThemUp: AI/Services/STUFindEnemyService.cpp
    1// TickNode
    2UE_LOG(LogTemp, Display, TEXT("find enemy"));
  • 游戏角色死亡后输入日志

    ShootThemUp: Components/STUHealthComponent.cpp
    1// OnTakeAnyDamage
    2UE_LOG(LogTemp, Display, TEXT("character dead"));

设置NPC生命值

BP_STUAICharacter > Health Component > Details > Max Health

设置为5


查看

NPC死亡后, 行为树仍在运行: 直到Character的Destroy函数被调用



AIController::RunBehaviorTree

  1. UBehaviorTreeComponent派生自BrainComponent
  2. 调用UBrainComponent::StartTree实现

UBrainComponent::Cleanup

停止行为树运行


在AICharacter中覆写OnDeath


修改基类声明

  • 使之为虚函数
  • 移动到protected

    派生类可以覆写基类私有函数, 但无法调用基类实现

ShootThemUp: Player/STUBaseCharacter.h

1virtual void OnDeath();

在派生类覆写

protected

ShootThemUp: AI/STUAICharacter.h

1virtual void OnDeath() override;

ShootThemUp: AI/STUAICharacter.cpp

 1#include "BrainComponent.h"
 2
 3void ASTUAICharacter::OnDeath()
 4{
 5    Super::OnDeath();
 6
 7    const auto STUController = Cast<AAIController>(Controller);
 8    if (STUController && STUController->BrainComponent)
 9    {
10        STUController->BrainComponent->Cleanup();
11    }
12}

查看

  • 设置步枪伤害

    BP_STURifleWeapon > Details > Damage Amount , 设置为10



移除日志打印

ShootThemUp: AI/Services/STUFindEnemyService.cpp

ShootThemUp: Components/STUHealthComponent.cpp

ShootThemUp: Weapon/STUBaseWeapon.cpp

1// GetTraceData

ShootThemUp: Weapon/STULauncherWeapon.cpp

1// MakeShot