当前位置: 首页 > news >正文

ASC学习笔记0017:返回此能力系统组件的所有属性列表

中文注释:UrealEngine-5.2.1源码-AbilitySystemComponent.h

学习内容:

/** 返回此能力系统组件的所有属性列表 */UFUNCTION(BlueprintPure, Category="Gameplay Attributes")void GetAllAttributes(TArray<FGameplayAttribute>& OutAttributes);

这个函数声明看起来是用于获取游戏能力系统中所有属性的方法。以下是关于这个函数的一些补充信息和使用示例:

函数说明

// 在头文件中的完整声明示例
UCLASS()
class YOURGAME_API UYourAbilitySystemComponent : public UAbilitySystemComponent
{GENERATED_BODY()public:/** 返回此能力系统组件的所有属性列表 */UFUNCTION(BlueprintPure, Category="Gameplay Attributes")void GetAllAttributes(TArray<FGameplayAttribute>& OutAttributes);
};

实现示例

void UYourAbilitySystemComponent::GetAllAttributes(TArray<FGameplayAttribute>& OutAttributes)
{OutAttributes.Reset();// 获取所有已注册的属性for (const FGameplayAttributeData* AttributeData : GetAttributeDataArray()){if (AttributeData && AttributeData->GetAttribute()){OutAttributes.Add(*AttributeData->GetAttribute());}}
}

Blueprint 使用示例

在蓝图中,你可以这样使用:

  1. 获取所有属性

    • 调用 Get All Attributes 节点

    • 输出到 Out Attributes 数组

  2. 遍历属性

// 伪代码示例
TArray<FGameplayAttribute> AllAttributes;
AbilitySystemComponent->GetAllAttributes(AllAttributes);for (const FGameplayAttribute& Attribute : AllAttributes)
{// 获取属性值float Value = AbilitySystemComponent->GetNumericAttribute(Attribute);// 打印属性名称和值FString AttributeName = Attribute.GetName();UE_LOG(LogTemp, Warning, TEXT("Attribute %s: %f"), *AttributeName, Value);
}

典型用途

  • UI 显示:在角色状态UI中显示所有属性

  • 调试目的:查看当前所有属性的状态

  • 属性比较:比较不同实体的属性配置

  • 序列化:保存和加载属性状态

注意事项

  • 这个函数返回的是属性定义(FGameplayAttribute),不是具体的数值

  • 要获取属性值,需要另外调用 GetNumericAttribute() 等方法

  • 确保在调用此函数时能力系统组件已正确初始化

在实际项目中,GetAllAttributes 函数有多种重要的应用场景。以下是一些具体的实际用例:

1. UI 属性显示系统

动态属性面板

// 在角色HUD或状态界面中
void UAttributeWidget::UpdateAttributeDisplay()
{TArray<FGameplayAttribute> Attributes;AbilitySystemComponent->GetAllAttributes(Attributes);for (const FGameplayAttribute& Attribute : Attributes){FAttributeDisplayInfo DisplayInfo;DisplayInfo.AttributeName = GetDisplayNameForAttribute(Attribute);DisplayInfo.CurrentValue = AbilitySystemComponent->GetNumericAttribute(Attribute);DisplayInfo.BaseValue = AbilitySystemComponent->GetNumericAttributeBase(Attribute);AddAttributeToPanel(DisplayInfo);}
}

设置界面 - 属性分配

// 角色升级或创建时的属性分配界面
void UAttributeAllocationWidget::PopulateAttributePoints()
{TArray<FGameplayAttribute> CoreAttributes;AbilitySystemComponent->GetAllAttributes(CoreAttributes);// 过滤出可分配的属性(如力量、敏捷、智力等)CoreAttributes = CoreAttributes.FilterByPredicate([](const FGameplayAttribute& Attr){return IsAllocatableAttribute(Attr);});CreateAttributeSlots(CoreAttributes);
}
 

2. 存档系统

保存所有属性状态

void UPlayerSaveGame::SaveAttributes(UAbilitySystemComponent* ASC)
{TArray<FGameplayAttribute> Attributes;ASC->GetAllAttributes(Attributes);SavedAttributes.Empty();for (const FGameplayAttribute& Attribute : Attributes){FSavedAttributeData SavedData;SavedData.Attribute = Attribute;SavedData.Value = ASC->GetNumericAttribute(Attribute);SavedData.BaseValue = ASC->GetNumericAttributeBase(Attribute);SavedAttributes.Add(SavedData);}
}
 

加载属性状态

void UPlayerSaveGame::LoadAttributes(UAbilitySystemComponent* ASC)
{for (const FSavedAttributeData& SavedData : SavedAttributes){ASC->SetNumericAttributeBase(SavedData.Attribute, SavedData.BaseValue);}
}
 

3. 调试和开发工具

开发者控制台命令

// 控制台命令:显示所有属性
void YourGameModule::ExecuteDumpAttributes(const TArray<FString>& Args)
{APawn* PlayerPawn = GetPlayerPawn();if (UAbilitySystemComponent* ASC = PlayerPawn->FindComponentByClass<UAbilitySystemComponent>()){TArray<FGameplayAttribute> Attributes;ASC->GetAllAttributes(Attributes);UE_LOG(LogConsoleResponse, Display, TEXT("=== Player Attributes ==="));for (const FGameplayAttribute& Attribute : Attributes){float Value = ASC->GetNumericAttribute(Attribute);UE_LOG(LogConsoleResponse, Display, TEXT("%s: %.1f"), *Attribute.GetName(), Value);}}
}
 

实时调试HUD

void UDebugHUD::DrawAttributeDebug()
{if (UAbilitySystemComponent* ASC = GetAbilitySystemComponent()){TArray<FGameplayAttribute> Attributes;ASC->GetAllAttributes(Attributes);float YPos = 100.f;for (const FGameplayAttribute& Attribute : Attributes){float Value = ASC->GetNumericAttribute(Attribute);FString Text = FString::Printf(TEXT("%s: %.1f"), *Attribute.GetName(), Value);DrawText(Text, FVector2D(10, YPos), FColor::White);YPos += 20.f;}}
}
 

4. AI 决策系统

基于属性的行为选择

void UCombatAIController::EvaluateCombatOptions()
{TArray<FGameplayAttribute> Attributes;AbilitySystemComponent->GetAllAttributes(Attributes);// 分析当前属性状态决定AI行为float HealthPercent = GetAttributePercent(UBaseAttributes::GetHealthAttribute());float ManaPercent = GetAttributePercent(UBaseAttributes::GetManaAttribute());if (HealthPercent < 0.3f){// 低血量时优先逃跑或使用防御技能ExecuteDefensiveBehavior();}else if (ManaPercent > 0.8f){// 高魔法值时使用强力技能ExecuteAggressiveBehavior();}
}
 

5. 成就和统计系统

属性达成成就

void UAchievementManager::CheckAttributeAchievements()
{TArray<FGameplayAttribute> Attributes;PlayerASC->GetAllAttributes(Attributes);for (const FGameplayAttribute& Attribute : Attributes){float Value = PlayerASC->GetNumericAttribute(Attribute);// 检查是否达到某个属性的成就条件if (Value >= GetAchievementThreshold(Attribute)){UnlockAchievement(FString::Printf(TEXT("Master_Of_%s"), *Attribute.GetName()));}}
}
 

6. 装备和物品系统

属性需求检查

bool UEquipmentComponent::CanEquipItem(const FItemData& Item)
{TArray<FGameplayAttribute> PlayerAttributes;AbilitySystemComponent->GetAllAttributes(PlayerAttributes);for (const FAttributeRequirement& Req : Item.AttributeRequirements){FGameplayAttribute* PlayerAttr = PlayerAttributes.FindByPredicate([&](const FGameplayAttribute& Attr){return Attr == Req.RequiredAttribute;});if (PlayerAttr && AbilitySystemComponent->GetNumericAttribute(*PlayerAttr) < Req.MinimumValue){return false; // 属性不足}}return true;
}
 

实际项目中的优化考虑

缓存机制

// 避免每帧调用 GetAllAttributes
void UAttributeMonitorComponent::CacheAttributes()
{if (bAttributesDirty){CachedAttributes.Empty();AbilitySystemComponent->GetAllAttributes(CachedAttributes);bAttributesDirty = false;}
}
 

属性变化监听

// 监听属性变化,只在变化时更新
void UAttributeMonitorComponent::OnAttributeChanged(const FOnAttributeChangeData& Data)
{// 标记需要更新缓存bAttributesDirty = true;// 立即处理变化的特定属性HandleAttributeChange(Data.Attribute, Data.NewValue);
}
 

这些实际应用展示了 GetAllAttributes 在游戏系统各个层面的重要性,从核心玩法到工具开发都有广泛用途。

http://www.dtcms.com/a/613835.html

相关文章:

  • Python可迭代对象讲解
  • 开源项目分享:Gitee热榜项目 2025年11月第三周 周榜
  • 哪里可做网站优化推广网站seo
  • Java Web 项目中Maven 常用库
  • 私人做网站图片网络科技有限公司怎么挣钱
  • 人力资源网站怎么做网络维护是什么职业
  • 体育直播/赛事直播/电竞直播/游戏直播/录播转播/原生APP/赛程比分系统
  • 学Java第四十五天——斗地主小游戏创作
  • 怎样创建一个国际网站wordpress外观菜单
  • 【总结】计网 IPv6
  • 【动态高斯重建】论文集合:从4DGT到OMG4、4DSioMo
  • 【ASR论文】Zipformer:更快、更强的语音识别编码器 | 小米公司
  • 从零开始学二叉树(上):树的初识 —— 从文件系统到树的基本概念
  • wordpress做网站卡吗服装企业 北京 网站建设
  • wordpress站点地址没更改wordpress 百秀主题
  • Foreign Function Interface
  • 在线C语言编译器 | 提供快速高效的C语言编程环境
  • 11月15日星期六今日早报简报微语报早读
  • 发电机组和负荷模型
  • 手机版 网站建设新闻今天
  • 节流throttle防抖debounce的函数封装
  • CSS 网格元素:构建现代网页布局的基石
  • 屹晶微 EG2134 三相独立半桥驱动芯片技术解析
  • 用py做网站写wordpress
  • 12. C语言高级编程-内存管理(2)
  • 【复习】计网每日一题1115---IPv6地址的简洁表示、::
  • RHCSA做项目:基于LAMP环境搭建Web应用(Discuz!论坛)的基础环境与部署流程
  • 南昌网站搭建公司 赣ICP温州排名推广
  • 前端使用TensorFlow.js reactjs调用本地模型 实现图像、文本、音频/声音、视频相关识别
  • 香蕉派 BPI-2K3000 工业计算机开发板采用龙芯2K3000芯片设计