7. UE5 RPG修改GAS的Attribute的值

前面几节文章介绍了如何在角色身上添加AbilitySystemComponent和AttributeSet。并且还实现了给AttributeSet添加自定义属性。接下来,实现一下如何去修改角色身上的Attribute的值。

实现拾取药瓶回血功能

首先创建一个继承于Actor的c++类,actor是可以放置到场景中的基类。

bash 复制代码
	UPROPERTY(VisibleAnywhere)
	TObjectPtr<UStaticMeshComponent> Mesh;

创建一个静态模型组件,用来显示当前可拾取物的模型。

bash 复制代码
	UPROPERTY(VisibleAnywhere)
	TObjectPtr<USphereComponent> Sphere;

创建一个碰撞体球,用于检测和主角的碰撞来触发回调。

bash 复制代码
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
	SetRootComponent(Mesh);

	Sphere = CreateDefaultSubobject<USphereComponent>("Sphere");
	Sphere->SetupAttachment(GetRootComponent());

然后初始化中,创建对象,并将Mesh设置为根节点,并将球碰撞体挂在Mesh下面。

bash 复制代码
UFUNCTION()
virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

UFUNCTION()
virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

创建两个回调,用于碰撞触发的开始和结束。

bash 复制代码
	Sphere->OnComponentBeginOverlap.AddDynamic(this, &AEffectActorBase::OnOverlap);
	Sphere->OnComponentEndOverlap.AddDynamic(this, &AEffectActorBase::EndOverlap);

绑定到球体碰撞事件上,如果球体触发了碰撞,则会调用这两个函数。

bash 复制代码
void AEffectActorBase::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	//TODO: 为了测试数值修改功能,启用了常量转变量功能。
	if(IAbilitySystemInterface* ASCInterface = Cast<IAbilitySystemInterface>(OtherActor))
	{
		 //根据类从ASC里面获取到对应的AS实例
		const UAttributeSetBase* AttributeSet = Cast<UAttributeSetBase>(ASCInterface->GetAbilitySystemComponent()->GetAttributeSet(UAttributeSetBase::StaticClass()));
		UAttributeSetBase* MutableAttributeSet = const_cast<UAttributeSetBase*>(AttributeSet); //将常量转为变量
		MutableAttributeSet->SetHealth(AttributeSet->GetHealth() + 25.f);
		Destroy(); // 销毁自身
	}
}

接着在碰撞触发的时候,从接口获取到AttributeSet,然后设置数值增长。

接下来在UE里面创建一个蓝图,基于EffectActorBase。

左侧会发现我们在代码中添加的Mesh和Sphere。

添加模型网格体,然后调整球的大小。

运行场景,输入showdebug abilitysystem

如果值修改,那证明功能实现。

EffectActorBase.h

bash 复制代码
// 版权归暮志未晚所有。

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "EffectActorBase.generated.h"

class USphereComponent;
class UStaticMeshComponent;

UCLASS()
class AURA_API AEffectActorBase : public AActor
{
	GENERATED_BODY()
	
public:	
	AEffectActorBase();

	UFUNCTION()
	virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION()
	virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
	
protected:
	// 游戏开始或生成对象时回调
	virtual void BeginPlay() override;

private:
	UPROPERTY(VisibleAnywhere)
	TObjectPtr<USphereComponent> Sphere;

	UPROPERTY(VisibleAnywhere)
	TObjectPtr<UStaticMeshComponent> Mesh;
};

EffectActorBase.app

bash 复制代码
// 版权归暮志未晚所有。


#include "Actor/EffectActorBase.h"

#include "AbilitySystemComponent.h"
#include "AbilitySystemInterface.h"
#include "AbilitySystem/AttributeSetBase.h"
#include "Components/SphereComponent.h"

AEffectActorBase::AEffectActorBase()
{
 	// 设置当前对象是否每帧调用Tick()
	PrimaryActorTick.bCanEverTick = false;

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
	SetRootComponent(Mesh);

	Sphere = CreateDefaultSubobject<USphereComponent>("Sphere");
	Sphere->SetupAttachment(GetRootComponent());
}

void AEffectActorBase::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	//TODO: 为了测试数值修改功能,启用了常量转变量功能。
	if(IAbilitySystemInterface* ASCInterface = Cast<IAbilitySystemInterface>(OtherActor))
	{
		 //根据类从ASC里面获取到对应的AS实例
		const UAttributeSetBase* AttributeSet = Cast<UAttributeSetBase>(ASCInterface->GetAbilitySystemComponent()->GetAttributeSet(UAttributeSetBase::StaticClass()));
		UAttributeSetBase* MutableAttributeSet = const_cast<UAttributeSetBase*>(AttributeSet); //将常量转为变量
		MutableAttributeSet->SetHealth(AttributeSet->GetHealth() + 25.f);
		Destroy(); // 销毁自身
	}
}

void AEffectActorBase::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}

void AEffectActorBase::BeginPlay()
{
	Super::BeginPlay();

	Sphere->OnComponentBeginOverlap.AddDynamic(this, &AEffectActorBase::OnOverlap);
	Sphere->OnComponentEndOverlap.AddDynamic(this, &AEffectActorBase::EndOverlap);
}
相关推荐
从零开始学习人工智能1 小时前
从游戏到自动驾驶:互联网时代强化学习如何让机器学会自主决策?
人工智能·游戏·自动驾驶
豆沙沙包?13 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
程序员大辉19 小时前
游戏常用运行库合集 | GRLPackage 游戏运行库!
游戏
不伤欣1 天前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
笑鸿的学习笔记1 天前
虚幻引擎5-Unreal Engine笔记之SET节点的输出引脚获取设置后的最新变量值
笔记·ue5·虚幻
Zhichao_971 天前
【UE5.1 C++】VS2022下载安装
ue5
阿阳微客1 天前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
m0_552200822 天前
《UE5_C++多人TPS完整教程》学习笔记37 ——《P38 变量复制(Variable Replication)》
c++·游戏·ue5
曹勖之2 天前
在 UE5 蓝图中配置Actor类型的Asset以作为位置和旋转设置目标
ue5·机器人
曹勖之2 天前
UE 5 和simulink联合仿真,如果先在UE5这一端结束Play,过一段时间以后**Unreal Engine 5** 中会出现显存不足错误
matlab·ue5·机器人