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

相关推荐
CS_浮鱼7 小时前
【Linux】基础IO
linux·运维·chrome
序属秋秋秋7 小时前
《Linux系统编程之进程基础》【进程状态】
linux·运维·c语言·c++·笔记·操作系统·进程状态
BS_Li7 小时前
【Linux系统编程】进程控制
java·linux·数据库
因为奋斗超太帅啦7 小时前
Git分布式版本控制工具学习笔记(一)——git本地仓库的基本使用
笔记·git·学习
Jeled7 小时前
RecyclerView ViewHolder 复用机制详解(含常见错乱问题与优化方案)
android·学习·面试·kotlin
可可苏饼干8 小时前
LVS服务器
linux·运维·笔记·学习·lvs
四谎真好看8 小时前
Java 黑马程序员学习笔记(进阶篇27)
java·开发语言·笔记·学习·学习笔记
艾莉丝努力练剑9 小时前
【Linux基础开发工具 (三)】Vim从入门到精通(下):效率翻倍的编辑技巧与个性化配置攻略
linux·运维·服务器·c++·ubuntu·centos·vim
Crazy________10 小时前
40nginx从单节点 HTTPS 到集群负载均衡
linux·运维·服务器
悟空码字13 小时前
部署Spring Boot项目到Linux服务器数据盘
linux·spring boot·部署·数据盘