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

ASC学习笔记0015:此能力系统组件是否具有此属性?

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

学习内容:

/*** 此能力系统组件是否具有此属性?* * @param Attribute	要从中检索目标标签的游戏效果句柄* * @return 如果Attribute有效且此能力系统组件包含一个包含Attribute的属性集,则返回true。否则返回false。*/bool HasAttributeSetForAttribute(FGameplayAttribute Attribute) const;

这个函数用于检查能力系统组件是否拥有与指定游戏属性(FGameplayAttribute)相关联的属性集。

函数说明

作用: 验证能力系统组件是否包含管理指定属性所需的属性集。

参数:

  • Attribute: 要检查的游戏属性句柄

返回值:

  • true: 如果属性有效且组件包含对应的属性集

  • false: 如果属性无效或组件没有对应的属性集

使用场景示例

// 假设我们有一个生命值属性
FGameplayAttribute HealthAttribute = UMyAttributeSet::GetHealthAttribute();// 检查能力系统组件是否有管理生命值的属性集
if (AbilitySystemComponent->HasAttributeSetForAttribute(HealthAttribute))
{// 可以安全地使用该属性float CurrentHealth = AbilitySystemComponent->GetGameplayAttributeValue(HealthAttribute);
}
else
{// 组件不支持此属性,需要处理错误情况UE_LOG(LogTemp, Warning, TEXT("Attribute set for health not found"));
}

底层实现逻辑

通常这个函数会:

  1. 验证输入的 Attribute 是否有效

  2. 在组件内部查找管理该属性的属性集

  3. 返回是否存在对应的属性集

注意事项

  • 在使用任何属性相关操作前,最好先用此函数进行检查

  • 这有助于避免在运行时出现空指针或无效属性访问

  • 属性集通常是在初始化能力系统组件时注册的

这个函数是GAS(游戏能力系统)框架中重要的安全检查机制。

在实际项目中,HasAttributeSetForAttribute 主要用于安全访问检查系统兼容性验证。以下是几个典型应用场景:

1. UI系统 - 动态属性显示

// 在HUD或属性UI中安全地显示属性
void UPlayerHUDWidget::UpdateAttributeDisplay()
{if (AbilitySystemComponent->HasAttributeSetForAttribute(UBaseAttributes::GetHealthAttribute())){float Health = AbilitySystemComponent->GetNumericAttribute(UBaseAttributes::GetHealthAttribute());HealthBar->SetPercent(Health / MaxHealth);HealthText->SetText(FText::AsNumber(Health));}else{HealthBar->SetVisibility(ESlateVisibility::Collapsed);}// 检查是否有法力系统if (!AbilitySystemComponent->HasAttributeSetForAttribute(UBaseAttributes::GetManaAttribute())){ManaBar->SetVisibility(ESlateVisibility::Collapsed);ManaText->SetVisibility(ESlateVisibility::Collapsed);}
}
 

2. 技能系统 - 条件性技能释放

// 检查技能消耗是否可行
bool UFireballSpell::CanActivateAbility() const
{if (!Super::CanActivateAbility())return false;// 检查是否有法力属性集if (!AbilitySystemComponent->HasAttributeSetForAttribute(UBaseAttributes::GetManaAttribute())){return false; // 这个角色没有法力系统,不能释放需要法力的技能}float CurrentMana = AbilitySystemComponent->GetNumericAttribute(UBaseAttributes::GetManaAttribute());return CurrentMana >= ManaCost;
}
 

3. 角色初始化 - 属性系统验证

// 角色初始化时验证必要的属性集
void ABaseCharacter::InitializeAttributes()
{if (!AbilitySystemComponent)return;// 验证核心属性集是否存在TArray<FGameplayAttribute> RequiredAttributes = {UBaseAttributes::GetHealthAttribute(),UBaseAttributes::GetMaxHealthAttribute(),UBaseAttributes::GetStaminaAttribute()};for (FGameplayAttribute Attr : RequiredAttributes){if (!AbilitySystemComponent->HasAttributeSetForAttribute(Attr)){UE_LOG(LogGame, Error, TEXT("Missing required attribute set for %s"), *Attr.GetName());return;}}// 所有必要属性都存在,继续初始化...InitializeDefaultAttributes();
}
 

4. 多人游戏 - 网络同步验证

// 在网络RPC中验证客户端请求
void APlayerCharacter::Server_UseHealthPotion_Implementation()
{if (!AbilitySystemComponent->HasAttributeSetForAttribute(UBaseAttributes::GetHealthAttribute())){// 客户端可能处于异常状态,没有健康属性集Client_OnPotionUseFailed(EPotionFailReason::InvalidCharacterState);return;}float CurrentHealth = AbilitySystemComponent->GetNumericAttribute(UBaseAttributes::GetHealthAttribute());float MaxHealth = AbilitySystemComponent->GetNumericAttribute(UBaseAttributes::GetMaxHealthAttribute());if (CurrentHealth >= MaxHealth){Client_OnPotionUseFailed(EPotionFailReason::AlreadyAtFullHealth);return;}// 应用治疗效果...ApplyHealthPotionEffect();
}
 

5. 装备系统 - 属性加成检查

// 装备物品前检查属性兼容性
bool UEquipmentManager::CanEquipItem(const UEquipmentItem* Item) const
{if (!Item || !AbilitySystemComponent)return false;// 检查装备提供的属性加成是否与角色属性系统兼容for (const FAttributeModifier& Modifier : Item->AttributeModifiers){if (!AbilitySystemComponent->HasAttributeSetForAttribute(Modifier.Attribute)){UE_LOG(LogGame, Warning, TEXT("Cannot equip item: character lacks attribute %s"), *Modifier.Attribute.GetName());return false;}}return true;
}
 

6. AI系统 - 行为树条件检查

// AI行为树中的服务节点
UBTService_CheckAttributes::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);AAIController* AIController = OwnerComp.GetAIOwner();ABaseCharacter* Character = Cast<ABaseCharacter>(AIController->GetPawn());if (Character && Character->GetAbilitySystemComponent()){UAbilitySystemComponent* ASC = Character->GetAbilitySystemComponent();// 检查是否有生命值属性来决定是否逃跑if (ASC->HasAttributeSetForAttribute(UBaseAttributes::GetHealthAttribute())){float Health = ASC->GetNumericAttribute(UBaseAttributes::GetHealthAttribute());bool bShouldFlee = Health < FleeHealthThreshold;OwnerComp.GetBlackboardComponent()->SetValueAsBool(ShouldFleeKey, bShouldFlee);}}
}
 

7. 调试和开发工具

// 开发时显示角色属性状态
void ADebugCharacter::DisplayAttributeInfo()
{if (!AbilitySystemComponent)return;TArray<UAttributeSet*> AttributeSets;AbilitySystemComponent->GetAllAttributeSets(AttributeSets);for (UAttributeSet* Set : AttributeSets){FString SetName = Set->GetClass()->GetName();UE_LOG(LogGame, Log, TEXT("Attribute Set: %s"), *SetName);// 检查每个属性集管理的属性// ...}// 检查特定属性是否存在FGameplayAttribute TestAttributes[] = {UBaseAttributes::GetHealthAttribute(),UBaseAttributes::GetManaAttribute(),UBaseAttributes::GetStaminaAttribute()};for (FGameplayAttribute Attr : TestAttributes){bool bHasAttribute = AbilitySystemComponent->HasAttributeSetForAttribute(Attr);UE_LOG(LogGame, Log, TEXT("Attribute %s: %s"), *Attr.GetName(), bHasAttribute ? TEXT("Present") : TEXT("Missing"));}
}
 

实际项目中的最佳实践

  1. 防御性编程: 在访问属性前总是进行检查

  2. 性能考虑: 避免在每帧中频繁调用,必要时缓存结果

  3. 错误处理: 提供有意义的错误信息和回退方案

  4. 扩展性: 支持不同角色类型拥有不同的属性集组合

这个函数在构建健壮的GAS系统中非常重要,特别是在有多种角色类型(玩家、NPC、怪物等)的项目中,每个角色类型可能拥有不同的属性集合。

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

相关文章:

  • 如何基于DSL脚本进行elasticsearch向量检索示例
  • 如何利用大语言模型(LLM)实现自动标注与内容增强
  • h5网站有哪些网页设计公司的目标客户有哪些
  • 做网站推广合同做标志的网站
  • 【ZeroRange WebRTC】Amazon Kinesis Video Streams WebRTC Data Plane REST API 深度解析
  • 2025从部署到迭代:Deepseek知识库部署服务商如何护航企业知识管理?
  • 检索增强微调(RAFT)如何重塑慢病健康管理?——从技术原理到落地实践
  • 网站开发 工作量评估关于建设网站安全性合同
  • 学Java第四十四天——可变参数、Collections工具类
  • XQuery FLWOR + HTML:深入理解与实际应用
  • 风丘助力优化ADAS测试:多路雷达、视频及车辆总线数据的集成处
  • LeetCode 152. 乘积最大子数组
  • XTDIC-SPARK高速3D测量系统在电子产品跌落测试中的动态变形与可靠性评估
  • Java设计模式之原型模式深度解析
  • 设置网站关键词怎么做高端大气网站源码
  • 网站内链建设不可忽视的地方零基础短视频制作培训
  • php做企业网站管理系统网络营销的特征和功能
  • 【WSL】继承Windows科学流量
  • 可复用的 Vue 3 + TypeScript + Element Plus 的列表请求(列表页/表格)通用方案
  • 安装 Composer
  • 国外做名片的网站网站没有备案
  • 解决VMware Workstation虚拟机中添加硬盘时找不到U盘对应的磁盘physicaldrive3
  • 解决 “Could not locate zlibwapi.dll” 错误全流程分析
  • 第一模板ppt免费下载seo人员工作内容
  • 【高级机器学习】 7. 带噪声数据的学习:从 MLE 到 MAP
  • 横沥镇做网站北京公司注册地址出租
  • 北湖区网站建设哪个好中网互联网站建设
  • @Autowired和@Resource的区别
  • MongoDB | python操作MongoDB的基础使用
  • 【C++进阶】异常