教程链接: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;
}
行为树如图所示
文章来源:https://www.toymoban.com/news/detail-510137.html
EQS的设置看教程的来,我还没研究透,研究透了单独写一个EQS的文章,把里头的随机点如何得分详实地写出来。文章来源地址https://www.toymoban.com/news/detail-510137.html
到了这里,关于UE5.1.1 C++从0开始(15.作业4个人作业分享)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!