UE4/5 GAS技能系统入门2 - AttributeSet

在GAS系统中对属性进行修改需要用到GE(Gameplay Effect),而这又涉及到AttributeSet这样的概念。

AttributeSet用于描述角色的属性集合,如攻击力、血量、防御力等,与GAS系统整合度较高,本文就来讲一讲AttributeSet的使用。

1.创建AttributeSet C++类

用蓝图也可以继承UAttributeSet创建相关类,但是到GE中并不会读取蓝图AttributeSet的字段并且也没有相关设置选项。
因此,为了能正常读到属性参数,需要通过C++去创建AttributeSet。

这里将AttributeSet命名为MyAttributeSet.h。

MyAttributeSet.h

cpp 复制代码
#pragma once

#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "AbilitySystemComponent.h"
#include "MyAttributeSet.generated.h"

// Uses macros from AttributeSet.h

#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
    GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
    GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
    GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
    GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)

/**
 *
 */
UCLASS()
class MYPROJECT2_API UMyAttributeSet : public UAttributeSet
{
    GENERATED_BODY()

public:
    // Attributes
    UPROPERTY(VisibleAnywhere, BlueprintReadWrite);
    FGameplayAttributeData Health;
    ATTRIBUTE_ACCESSORS(UMyAttributeSet, Health);

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite);
    FGameplayAttributeData MaxHealth;
    ATTRIBUTE_ACCESSORS(UMyAttributeSet, MaxHealth);

};

上述脚本定义了Health和MaxHealth两个属性,无需定义cpp文件,保证相关模块依赖已定义,直接编译即可。

编译后在GE中即可读到相关属性:

2.Attribute Set的数据配置

回到Default Pawn玩家类,检查是否挂载AbilitySystemComponent组件:

选中该组件,设置AttributeSet:

新建一个DataTable,为属性配置初始值,表的类型设置为AttributeMetaData

双击打开配置表,配置初始数据。此处需要注意字段要带上类名,否则不生效

回到角色蓝图,添加配置表文件:

3.Attribute Set数据获取

AttributeSet数据不能只通过GE操作,还需要额外拿出来暴露给UI显示。

使用节点 Get Float Attribute from Ability System Component即可获取数据,但似乎只有float类型:

最后调试一下:


参考:https://zhuanlan.zhihu.com/p/486808688

相关推荐
毕竟是shy哥8 小时前
CLIP:从自然语言监督中学习可迁移的视觉模型
学习
键盘上的猫头鹰8 小时前
【Linux 基础教程(一)】概述、安装与网络配置:VMware + CentOS + NAT + XShell 远程连接
linux·网络·centos
川石课堂软件测试8 小时前
作为一名测试工程师如何学习Kubernetes(k8s)技能
学习·测试工具·容器·职场和发展·kubernetes·测试用例·harmonyos
枳实-叶8 小时前
【Linux驱动开发】第18天:I2C驱动深度解析
linux·运维·驱动开发
shandianchengzi8 小时前
【记录】Ubuntu|Ubuntu 26.04 笔记本耗电过快,排查 省电过程
linux·运维·ubuntu
tryqaaa_8 小时前
学习日志(五)【php反序列化全加例题】【pop链,字符逃逸,session,伪协议】
android·学习·php·web·pop·session
li星野8 小时前
FastAPI 参数详解:路径参数、查询参数与请求体 —— 从入门到实战
服务器·学习·fastapi
陳10308 小时前
Linux:信号
linux·运维·服务器
承渊政道8 小时前
【MySQL数据库学习】(MySQL数据类型)
数据库·学习·mysql·ubuntu·bash·数据库开发·数据库系统
小此方8 小时前
Re:Linux系统篇(二十五)进程篇·十:深度硬核!Linux 进程等待,从 task_struct 源码到位图状态解构
linux·运维·驱动开发