UE5.1.1 C++从0开始(15.作业4个人作业分享)

教程链接: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的文章,把里头的随机点如何得分详实地写出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚江_wog1st

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值