You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unreal Engine中BTService无法更新Blackboard Key值问题

UE行为树BTService更新黑板血量值仅首次生效问题

问题背景

我正在为游戏中的AI敌人开发低血量“撤退”逻辑,为此创建了一个BTService,用于实时获取角色Health值并存储到Blackboard Key中。
目前遇到异常:该服务仅能首次正确设置“Health”值,后续不再更新,即使敌人血量下降,Blackboard Key仍固定为初始值不变。

已知现象

  • ShooterCharacter类中定义有Health变量
  • 对应AI的行为树中设有同名“Health” Blackboard Key,用于存储ShooterCharacter类的Health值
  • 为实现值同步逻辑,开发了名为“Update Character Health”的BTService
  • 游戏启动时该服务运行符合预期,行为树中Health键值正确显示为ShooterCharacter的初始血量70
  • 后续服务停止生效,即使对应ShooterCharacter实例的血量降至40,键值仍停留在70

相关实现代码

BTService_CharacterHealth.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "BehaviorTree/Services/BTService_BlackboardBase.h"
#include "BTService_CharacterHealth.generated.h"

/**
 * s
 */
UCLASS()
class SIMPLESHOOTER_API UBTService_CharacterHealth : public UBTService_BlackboardBase
{
    GENERATED_BODY()

public: 
    UBTService_CharacterHealth();

protected: 
    virtual void TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds) override;   
};

BTService_CharacterHealth.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "BTService_CharacterHealth.h"
#include "AIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "GameFramework/Pawn.h"
#include "Kismet/GameplayStatics.h"
#include "ShooterCharacter.h"

UBTService_CharacterHealth::UBTService_CharacterHealth()
{
    NodeName = "Update Character Health";
}

void UBTService_CharacterHealth::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
    Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

    AShooterCharacter*  Character = Cast<AShooterCharacter>(OwnerComp.GetAIOwner()->GetPawn());

    if (Character == nullptr)
    {
        return;
    }

    OwnerComp.GetBlackboardComponent()->SetValueAsFloat(GetSelectedBlackboardKey(), Character->Health);
};

排查进展

我最初怀疑问题与OwnerComp.GetBlackboardComponent()->SetValueAsFloat(GetSelectedBlackboardKey(), Character->Health);语句相关,于是在AShooterCharacter* Character = Cast<AShooterCharacter>(OwnerComp.GetAIOwner()->GetPawn());语句后添加了如下日志:

UE_LOG(LogTemp, Warning, TEXT("Character Health: %f"), Character->Health);

日志可正常实时输出最新血量值,当角色血量为40时会正确打印40,但即使日志正常输出,Blackboard Key的值仍停留在70不变。目前暂未定位到该问题的根因,可按需补充其他相关信息。


内容的提问来源于stack exchange,提问作者jovandl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 11:15:42