教程鏈接:https://www.bilibili.com/video/BV1nU4y1X7iQ
好吧這個作業(yè)應該是之前寫的,但是我發(fā)現(xiàn)我沒寫,后面我又回去自己寫了一遍再看代碼,感覺上大差不差,各位可以看著我的和老師的還有自己的對比下。
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;
}
行為樹如圖所示
文章來源:http://www.zghlxwxcb.cn/news/detail-510137.html
EQS的設置看教程的來,我還沒研究透,研究透了單獨寫一個EQS的文章,把里頭的隨機點如何得分詳實地寫出來。文章來源地址http://www.zghlxwxcb.cn/news/detail-510137.html
到了這里,關于UE5.1.1 C++從0開始(15.作業(yè)4個人作業(yè)分享)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!