教程链接:https://www.bilibili.com/video/BV1nU4y1X7iQ
好吧这个作业应该是之前写的,但是我发现我没写,后面我又回去自己写了一遍再看代码,感觉上大差不差,各位可以看着我的和老师的还有自己的对比下。
SBTService_CheckHealth.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTService.h"
#include "SBTService_CheckHealth.generated.h"
/**
*
*/
UCLASS()
class ACTIONROGUELIKE_API USBTService_CheckHealth : public UBTService
{
GENERATED_BODY()
protected:
UPROPERTY(EditAnywhere,Category="AI")
FBlackboardKeySelector CheckHealthKey;
protected:
virtual void TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds) override;
};
SBTService_CheckHealth.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/SBTService_CheckHealth.h"
#include "AIModule/Classes/AIController.h"
#include "AI/SAICharacter.h"
#include "SAttributeComponent.h"
#include "AIModule/Classes/BehaviorTree/BlackboardComponent.h"
void USBTService_CheckHealth::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
//获得黑板组件,获取上面的键值
UBlackboardComponent* BlackboardComponent = OwnerComp.GetBlackboardComponent();
//先获得AI的角色
//我这里就不像教程写的那样每一步都用一个ensure了
AAIController* MyController = OwnerComp.GetAIOwner();
ASAICharacter* AICharacter = Cast<ASAICharacter>(MyController->GetCharacter());
//拿到我们的attributecomp
USAttributeComponent* AttributeComp = USAttributeComponent::GetArrtibutes(AICharacter);
if (AttributeComp->GetHealth()<=50.0f)
{
BlackboardComponent->SetValueAsBool(CheckHealthKey.SelectedKeyName, true);
}
else {
BlackboardComponent->SetValueAsBool(CheckHealthKey.SelectedKeyName, false);
}
}
SBTTask_HealSelf.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "SBTTask_HealSelf.generated.h"
/**
*
*/
UCLASS()
class ACTIONROGUELIKE_API USBTTask_HealSelf : public UBTTaskNode
{
GENERATED_BODY()
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
};
SBTTask_HealSelf.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/SBTTask_HealSelf.h"
#include "AI/SAICharacter.h"
#include "AIModule/Classes/AIController.h"
#include "SAttributeComponent.h"
EBTNodeResult::Type USBTTask_HealSelf::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
//获得Controller
AAIController* MyController = OwnerComp.GetAIOwner();
//获得Character
ASAICharacter* MyPawn = Cast<ASAICharacter>(MyController->GetCharacter());
if (ensure(MyPawn))
{
USAttributeComponent* MyAttributeComp = USAttributeComponent::GetArrtibutes(MyPawn);
MyAttributeComp->ApplyHealthChange(MyPawn, MyAttributeComp->getHealthMax());
return EBTNodeResult::Succeeded;
}
return EBTNodeResult::Failed;
}
行为树如图所示
EQS的设置看教程的来,我还没研究透,研究透了单独写一个EQS的文章,把里头的随机点如何得分详实地写出来。