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

P48-56 应用游戏标签

这一段课主要是把每种道具的游戏Tag进行了整理与应用

AuraAbilitySystemComponentBase.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "AbilitySystemComponent.h" #include "AuraAbilitySystemComponentBase.generated.h" DECLARE_MULTICAST_DELEGATE_OneParam(FEffectAssTags, const FGameplayTagContainer& /*AssetTags*/) /** * */ UCLASS() class MYGAS_API UAuraAbilitySystemComponentBase : public UAbilitySystemComponent { GENERATED_BODY() protected: void EffectApplied(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayEffectSpec& GameplayEffectSpec, FActiveGameplayEffectHandle ActiveGameplayEffectHandle); public: void AbilityActorInfoSet(); FEffectAssTags EffectAssetTags; };

AuraAbilitySystemComponentBase.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "AbilitySystem/AuraAbilitySystemComponentBase.h" void UAuraAbilitySystemComponentBase::AbilityActorInfoSet() { OnGameplayEffectAppliedDelegateToSelf.AddUObject(this,&UAuraAbilitySystemComponentBase::EffectApplied); } void UAuraAbilitySystemComponentBase::EffectApplied(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayEffectSpec& GameplayEffectSpec, FActiveGameplayEffectHandle ActiveGameplayEffectHandle) { FGameplayTagContainer TagContainer; GameplayEffectSpec.GetAllAssetTags(TagContainer); EffectAssetTags.Broadcast(TagContainer); }

AuraEffectActor.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameplayEffect.h" #include "AbilitySystem/AuraAbilitySystemComponentBase.h" #include "Components/SphereComponent.h" #include "GameFramework/Actor.h" #include "GameplayEffectTypes.h" #include "AuraEffectActor.generated.h" class UGameplayEffect; class UAbilitySystemComponent; UENUM(BlueprintType) enum class EEffectApplicationPolicy : uint8 { ApplyOnOverlap,ApplyOnEndOverlap,DoNotApply }; UENUM(BlueprintType) enum class EEffectRemovePolicy : uint8 { RemoveOnEndOverlap,DoNotRemove }; UCLASS() class MYGAS_API AAuraEffectActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AAuraEffectActor(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; UFUNCTION(BlueprintCallable) void ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass); UFUNCTION(BlueprintCallable) void OnOverlap(AActor* TargetActor); UFUNCTION(BlueprintCallable) void OnEndOverlap(AActor* TargetActor); UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") TSubclassOf<UGameplayEffect> InstantGameplayEffectClass; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") EEffectApplicationPolicy InstantEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") TSubclassOf<UGameplayEffect> DurationGameplayEffectClass; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") EEffectApplicationPolicy DurationEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") TSubclassOf<UGameplayEffect> InfiniteGameplayEffectClass; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") EEffectApplicationPolicy InfiniteEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") EEffectRemovePolicy InfiniteEffectRemovePolicy = EEffectRemovePolicy::RemoveOnEndOverlap; TMap<FActiveGameplayEffectHandle , UAbilitySystemComponent*> ActiveEffectHandles; UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects") float ActorLevel = 1.f; private: };

AuraEffectActor.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "Actor/AuraEffectActor.h" #include "AbilitySystemBlueprintLibrary.h" #include "AbilitySystemComponent.h" #include "AbilitySystemInterface.h" #include "AbilitySystem/AuraAbilitySystemComponentBase.h" #include "AbilitySystem/AuraAttributeSet.h" #include "Components/StaticMeshComponent.h" // Sets default values AAuraEffectActor::AAuraEffectActor() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = false; SetRootComponent(CreateDefaultSubobject<USceneComponent>("SceneRoot")); } // Called when the game starts or when spawned void AAuraEffectActor::BeginPlay() { Super::BeginPlay(); } void AAuraEffectActor::ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass) { //这里是使用 UAbilitySystemBlueprintLibrary 内的静态函数,在指定的Target上查找并返回UAbilitySystemComponent UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor); if(TargetASC==nullptr) return; check(GameplayEffectClass); //创建了一个游戏效果的内容句柄,提供游戏效果的信息 FGameplayEffectContextHandle TargetASCContext =TargetASC->MakeEffectContext(); //把自己作为游戏效果的源对象,确保AbilitySystem可以识别到自己 TargetASCContext.AddSourceObject(this); //这里创建了一个游戏效果规范,GameplayEffectClass 是要应用的游戏效果的类,1.0f 是游戏效果的初始生命周期倍率,TargetASCContext 是游戏效果的上下文. 调用 MakeOutgoingSpec 函数,将会根据提供的参数创建一个游戏效果规范 const FGameplayEffectSpecHandle EffectSpecHandle = TargetASC->MakeOutgoingSpec(GameplayEffectClass,ActorLevel,TargetASCContext); const FActiveGameplayEffectHandle ActiveGameplayEffectHandle = TargetASC->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get()); const bool bIsInfinite = EffectSpecHandle.Data.Get()->Def.Get()->DurationPolicy == EGameplayEffectDurationType::Infinite; if(bIsInfinite) { ActiveEffectHandles.Add(ActiveGameplayEffectHandle,TargetASC); } } void AAuraEffectActor::OnOverlap(AActor* TargetActor) { if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap) { ApplyEffectToTarget(TargetActor,InstantGameplayEffectClass); } if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap) { ApplyEffectToTarget(TargetActor,DurationGameplayEffectClass); } if(InfiniteEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap) { ApplyEffectToTarget(TargetActor,InfiniteGameplayEffectClass); } } void AAuraEffectActor::OnEndOverlap(AActor* TargetActor) { if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap) { ApplyEffectToTarget(TargetActor,InstantGameplayEffectClass); } if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap) { ApplyEffectToTarget(TargetActor,DurationGameplayEffectClass); } if(InfiniteEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap) { ApplyEffectToTarget(TargetActor,InfiniteGameplayEffectClass); } if(InfiniteEffectRemovePolicy == EEffectRemovePolicy::RemoveOnEndOverlap) { //获取到目标角色的能力组件 UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor); if(!IsValid(TargetASC)) return; //创建了一个储存移除游戏效果句柄的数组 HandlesToRemove TArray<FActiveGameplayEffectHandle> HandlesToRemove; //遍历 ActiveEffectHandles 数组 for(TTuple<FActiveGameplayEffectHandle, UAbilitySystemComponent*> HandlePair:ActiveEffectHandles) { // 当 TargetASC 内有 HandlePair.Value的值的时候 if(TargetASC == HandlePair.Value) { // 移除 HandlePair 的效果 TargetASC->RemoveActiveGameplayEffect(HandlePair.Key, 1); //把这个效果放在 HandlesToRemove 中 HandlesToRemove.Add(HandlePair.Key); } } // 遍历 HandlesToRemove 内的每一个元素 for(auto& Handle : HandlesToRemove) { // 在 ActiveEffectHandles 这个数组内移除 ActiveEffectHandles.FindAndRemoveChecked(Handle); } } }

AuraCharacter.h

private: virtual void InitAbilityActorInfo() override;

AuraCharacter.cpp

void AAuraCharacter::InitAbilityActorInfo() { AAuraPlayerState* AuraPlayerState = GetPlayerState<AAuraPlayerState>(); check(AuraPlayerState); AuraPlayerState->GetAbilitySystemComponent()->InitAbilityActorInfo(AuraPlayerState,this); Cast<UAuraAbilitySystemComponentBase>(AuraPlayerState->GetAbilitySystemComponent())->AbilityActorInfoSet(); AbilitySystemComponent = AuraPlayerState->GetAbilitySystemComponent(); AttributesSet = AuraPlayerState->GetAttributeSet(); //当角色初始化的时候加载UI的初始化 if(AAuraPlayerController* AuraPlayerController = Cast<AAuraPlayerController>(GetController())) { //获得HUD AAuraHUD* AuraHUD = Cast<AAuraHUD>(AuraPlayerController->GetHUD()); AuraHUD->InitOverlay(AuraPlayerController,AuraPlayerState,AbilitySystemComponent,AttributesSet); } }

AuraCharacterBase.h

virtual void InitAbilityActorInfo();

AuraCharacterBase.cpp

void AAuraCharacterBase::InitAbilityActorInfo() { }

AuraEnemy.h

protected: virtual void BeginPlay() override; virtual void InitAbilityActorInfo() override; };

AuraEnemy.cpp

void AAuraEnemy::BeginPlay() { Super::BeginPlay(); InitAbilityActorInfo(); } void AAuraEnemy::InitAbilityActorInfo() { AbilitySystemComponent->InitAbilityActorInfo(this,this); Cast<UAuraAbilitySystemComponentBase>(AbilitySystemComponent)->AbilityActorInfoSet(); }

OverlayAuraWidgetController.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "IPropertyTable.h" #include "UI/WidgetController/AuraWidgetController.h" #include "OverlayAuraWidgetController.generated.h" USTRUCT(BlueprintType) struct FUIWidgetRow : public FTableRowBase { GENERATED_BODY() UPROPERTY(EditAnywhere,BlueprintReadOnly) FGameplayTag MessageTag = FGameplayTag(); UPROPERTY(EditAnywhere,BlueprintReadOnly) FText Message = FText(); UPROPERTY(EditAnywhere,BlueprintReadOnly) TSubclassOf<class UAuraUserWidget> MessageWidget; UPROPERTY(EditAnywhere,BlueprintReadOnly) UTexture2D* Image = nullptr; }; class UAuraUserWidget; DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnHealthChangedSignature, float, NewHealth); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMaxHealthChangedSignature,float,NewMaxHealth); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnManaChangedSignature,float,NewMana); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMaxManaChangedSignature,float,NewMaxMana); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMessageWidgetRowSingnature, FUIWidgetRow, Row); /** * */ UCLASS(BlueprintType,Blueprintable) class MYGAS_API UOverlayAuraWidgetController : public UAuraWidgetController { GENERATED_BODY() public: virtual void BroadcastInitialValues() override; virtual void BindCallbacksToDependencies() override; UPROPERTY(BlueprintAssignable,Category="GAS|Attributes") FOnHealthChangedSignature OnHealthChangedSignature; UPROPERTY(BlueprintAssignable,Category="GAS|Attributes") FOnMaxHealthChangedSignature OnMaxHealthChangedSignature; UPROPERTY(BlueprintAssignable,Category="GAS|Attributes") FOnManaChangedSignature OnManaChangedSignature; UPROPERTY(BlueprintAssignable,Category="GAS|Attributes") FOnMaxManaChangedSignature OnMaxManaChangedSignature; UPROPERTY(BlueprintAssignable,Category="GAS|Messages") FMessageWidgetRowSingnature MessageWidgetRowDelegate; protected: UPROPERTY(EditDefaultsOnly,BlueprintReadOnly,Category="Widget Data") TObjectPtr<UDataTable> MessageWidgetDataTable; void HealthChanged(const FOnAttributeChangeData& Data) const; void MaxHealthChanged(const FOnAttributeChangeData& Data) const; void ManaChanged(const FOnAttributeChangeData& Data) const; void MaxManaChanged(const FOnAttributeChangeData& Data) const; template<typename T> T* GetDataTableRowByTag(UDataTable* DataTable,const FGameplayTag& Tag); }; template <typename T> T* UOverlayAuraWidgetController::GetDataTableRowByTag(UDataTable* DataTable, const FGameplayTag& Tag) { return DataTable->FindRow<T>(Tag.GetTagName(), TEXT(""));; }

OverlayAuraWidgetController.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "UI/WidgetController/OverlayAuraWidgetController.h" #include "AbilitySystem/AuraAbilitySystemComponentBase.h" #include "AbilitySystem/AuraAttributeSet.h" #include "Engine/Engine.h" #include "GameFramework/Pawn.h" class UAuraAttributeSet; void UOverlayAuraWidgetController::BroadcastInitialValues() { //这里应该绑定一个事件,获取到AuraAttributeSet const UAuraAttributeSet* AuraAttributeSet = CastChecked<UAuraAttributeSet>(AttributeSet); // 获取到 Health 和 MaxHealth 属性,并进行广播 OnHealthChangedSignature.Broadcast(AuraAttributeSet->GetHealth()); OnMaxHealthChangedSignature.Broadcast(AuraAttributeSet->GetMaxHealth()); OnManaChangedSignature.Broadcast(AuraAttributeSet->GetMana()); OnMaxManaChangedSignature.Broadcast(AuraAttributeSet->GetMaxMana()); } void UOverlayAuraWidgetController::BindCallbacksToDependencies() { const UAuraAttributeSet* AuraAttributeSet = CastChecked<UAuraAttributeSet>(AttributeSet); //绑定血量 AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate( AuraAttributeSet->GetHealthAttribute()).AddUObject(this,&UOverlayAuraWidgetController::HealthChanged); AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate( AuraAttributeSet->GetMaxHealthAttribute()).AddUObject(this,&UOverlayAuraWidgetController::MaxHealthChanged); //绑定蓝量 AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate( AuraAttributeSet->GetManaAttribute()).AddUObject(this,&UOverlayAuraWidgetController::ManaChanged); AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate( AuraAttributeSet->GetMaxManaAttribute()).AddUObject(this,&UOverlayAuraWidgetController::MaxManaChanged); Cast<UAuraAbilitySystemComponentBase>(AbilitySystemComponent)->EffectAssetTags.AddLambda( [this](const FGameplayTagContainer& AssetTags) { for(const FGameplayTag& Tag : AssetTags) { //检查 MessageTag 是否是Data表内的 MessageTag,如何不是就会返回False FGameplayTag MessageTag = FGameplayTag::RequestGameplayTag(FName("Message")); if(Tag.MatchesTag(MessageTag)) { const FUIWidgetRow* Row = GetDataTableRowByTag<FUIWidgetRow>(MessageWidgetDataTable , Tag); MessageWidgetRowDelegate.Broadcast(*Row); } } } ); } void UOverlayAuraWidgetController::HealthChanged(const FOnAttributeChangeData& Data) const { OnHealthChangedSignature.Broadcast(Data.NewValue); } void UOverlayAuraWidgetController::MaxHealthChanged(const FOnAttributeChangeData& Data) const { OnMaxHealthChangedSignature.Broadcast(Data.NewValue); } void UOverlayAuraWidgetController::ManaChanged(const FOnAttributeChangeData& Data) const { OnManaChangedSignature.Broadcast(Data.NewValue); } void UOverlayAuraWidgetController::MaxManaChanged(const FOnAttributeChangeData& Data) const { OnMaxManaChangedSignature.Broadcast(Data.NewValue); }

回到蓝图内,创建DT_PrimaryAttibutes,结构类型是GameplayTagTableRow

在UI内创建DT_MessageWidgetData,结构类型是在OverlayAuraWidgetController.h内创建的结构体UIWidgetRow

在WidgetController内修改

在Project Setting内的Gameplay Tag内修改

相关文章:

  • PCIe控制逻辑介绍(一)
  • GitHub中多个PR时,如何协同合并和管理
  • 【计算机网络】TCP为什么可靠?解决了哪些问题?
  • JPress安装(Docker)
  • iMeta | 临床研究+scRNA-seq的组合思路 | 真实世界新辅助研究,HER2⁺就一定受益?单细胞揭示真正的“疗效敏感克隆”
  • 【BUG】mmdetection ValueError: need at least one array to concatenate
  • 【Qt4】Qt4中实现PDF预览
  • 【东枫科技】代理英伟达产品:智能网卡的连接线
  • URP - 深度图
  • CSS网格布局
  • UE5 ML机械学习肌肉反应与布料反应
  • 大疆三方云平台部署
  • Linux grep 命令详解及示例大全
  • 多线程“CPU 飙高”问题:如何确保配置的线程数与CPU核数匹配(Java、GoLang、Python )中的最佳实践解决方案
  • 可检查异常与不可检查异常
  • suna工具调用可视化界面实现原理分析(三)
  • 【神经网络、Transformer及模型微调】
  • Windows11下ESP-IDF开发环境搭建【基于Cursor/VS Code插件】
  • 2025-05-06 滑动窗口最大值
  • 逐次逼近式A/D转换器
  • 联想发布超级智能体矩阵,杨元庆:美国关税影响反映在产品定价上,未来不确定性很大
  • AI聊天机器人涉多起骚扰行为,专家呼吁加强伦理设计与监管
  • 中邮保险斥资8.69亿元举牌东航物流,持股比例达5%
  • 新疆生产建设兵团草湖项目区副主任宋全伟接受审查调查
  • 李云泽:支持设立新的金融资产投资公司,今天即将批复一家
  • 经济日报:落实落细更加积极的财政政策