P44,45 属性预处理,执行后游戏效果回调,附录指定区域内修改变量

这节课主要是怎么对Attribute进行在进行到游戏角色前先进行处理,以及游戏效果如何回调

AuraAttributeSet.h

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

 #include "CoreMinimal.h"
#include "AttributeSet.h"
#include "AbilitySystemComponent.h"
#include "GameFramework/Character.h"
#include "AuraAttributeSet.generated.h"


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

/**
 * 
 */
//这里是一个结构体
USTRUCT()
struct  FEffectProperties
{
   GENERATED_BODY()
   FEffectProperties(){}

   FGameplayEffectContextHandle EffectContextHandle;

   UPROPERTY()
   UAbilitySystemComponent* SourceASC = nullptr;

   UPROPERTY()
   AActor* SourceAvatarActor = nullptr;

   UPROPERTY()
   AController* SourceController = nullptr;

   UPROPERTY()
   ACharacter* SourceCharacter = nullptr;

   UPROPERTY()
   UAbilitySystemComponent* TargetASC = nullptr;

   UPROPERTY()
   AActor* TargetAvatarActor = nullptr;

   UPROPERTY()
   AController* TargetController = nullptr;

   UPROPERTY()
   ACharacter* TargetCharacter = nullptr;
};

UCLASS()
class MYGAS_API UAuraAttributeSet : public UAttributeSet
{
     GENERATED_BODY()
 public:
   UAuraAttributeSet();
   virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

   virtual void PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue) override;

   virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data) override;
   
   //创建AttributeData属性,并且是可以被复制的
   UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_Health ,Category="Vital Attributes")
   FGameplayAttributeData Health;
   ATTRIBUTE_ACCESSORS(UAuraAttributeSet,Health);
   
   UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_MaxHealth ,Category="Vital Attributes")
   FGameplayAttributeData MaxHealth;
   ATTRIBUTE_ACCESSORS(UAuraAttributeSet,MaxHealth);

   UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_Mana ,Category="Vital Attributes")
   FGameplayAttributeData Mana;
   ATTRIBUTE_ACCESSORS(UAuraAttributeSet,Mana);
   
   UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_MaxMana ,Category="Vital Attributes")
   FGameplayAttributeData MaxMana;
   ATTRIBUTE_ACCESSORS(UAuraAttributeSet,MaxMana);
   
   UFUNCTION()
   void OnRep_Health(const FGameplayAttributeData& OldHealth) const;

   UFUNCTION()
   void OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const;

   UFUNCTION()
   void OnRep_Mana(const FGameplayAttributeData& OldMana) const;

   UFUNCTION()
   void OnRep_MaxMana(const FGameplayAttributeData& OldMaxMana) const;

private:
   void SetEffectProperties(const FGameplayEffectModCallbackData& Data , FEffectProperties& Props) const;
};

AuraAttributeSet.cpp

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "AbilitySystem/AuraAttributeSet.h"

#include "AbilitySystemBlueprintLibrary.h"
#include "GameplayEffectExtension.h"
#include "GameFramework/Character.h"
#include "GameFramework/Pawn.h"
#include "Net/UnrealNetwork.h"
#include "PlayerController/AuraPlayerController.h"

UAuraAttributeSet::UAuraAttributeSet()
{
   InitHealth(50.f);
   InitMaxHealth(100.f);
   InitMana(25.f);
   InitMaxMana(50.f);
   
}

void UAuraAttributeSet::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
   //用来表示在Actor生命周期内要复制的属性,被创建并且复制的时候在网络上进行同步,确保客户端和服务端状态一致
   Super::GetLifetimeReplicatedProps(OutLifetimeProps);
   //这是一个宏,用来表现属性的复制条件和什么时候调用RepNotify,其中Cond指属性什么时候进行复制,Repnotify是指什么时候调用RepNotify函数,这里是Always,只要服务器接收到属性值就调用复制
   DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,Health,COND_None,REPNOTIFY_Always);
   
   DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,MaxHealth,COND_None,REPNOTIFY_Always);
   
// DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,Mana,COND_None,REPNOTIFY_Always);
   
// DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,MaxMana,COND_None,REPNOTIFY_Always);
   
}
//数值在应用到角色前进行修改 
void UAuraAttributeSet::PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue)
{
   Super::PreAttributeChange(Attribute, NewValue);
   //限制 health和Mana的最低值和最高值
   if(Attribute == GetHealthAttribute())
   {
      NewValue = FMath::Clamp(NewValue,0.f,GetMaxHealth());
   }
   if(Attribute == GetManaAttribute())
   {
      NewValue = FMath::Clamp(NewValue,0.f,GetMaxMana());
   }
}

void UAuraAttributeSet::SetEffectProperties(const FGameplayEffectModCallbackData& Data, FEffectProperties& Props) const
{
   // Source = causer  of the effect , Targetg = Target of the Effect (owner of this AS) 效果的上下文处理,包括来源和目标
   Props.EffectContextHandle = Data.EffectSpec.GetContext();
   // 获取效果的来源能力系统组件
   Props.SourceASC =Props.EffectContextHandle.GetOriginalInstigatorAbilitySystemComponent();
   // 如果有效,则获取来源的相关信息
   if(IsValid(Props.SourceASC) && Props.SourceASC->AbilityActorInfo.IsValid() && Props.SourceASC->AbilityActorInfo->AvatarActor.IsValid())
   {
      Props.SourceAvatarActor = Props.SourceASC->AbilityActorInfo->AvatarActor.Get();
      Props.SourceController = Props.SourceASC->AbilityActorInfo->PlayerController.Get();
      // 如果来源控制器为空且存在角色,则获取角色的控制器
      if(Props.SourceController == nullptr && Props.SourceAvatarActor != nullptr)
      {
         if(const APawn* Pawn = Cast<APawn>(Props.SourceAvatarActor))
         {
            Props.SourceController = Pawn->GetController();
         }
      }
      // 如果存在来源控制器,则尝试将其转换为角色
      if(Props.SourceController)
      {
         Props.SourceCharacter = Cast<ACharacter>(Props.SourceController->GetPawn());
      }
   }
   // 获取目标的相关信息
   if(Data.Target.AbilityActorInfo.IsValid() && Data.Target.AbilityActorInfo->AvatarActor.IsValid())
   {
      Props.TargetAvatarActor = Data.Target.AbilityActorInfo->AvatarActor.Get();
      Props.TargetController = Data.Target.AbilityActorInfo->PlayerController.Get();
      Props.TargetCharacter = Cast<ACharacter>(Props.TargetAvatarActor);
      Props.TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(Props.TargetAvatarActor);
      
   }
   
   //这里是用来Debug对生命值产生的数值
   /*if(Data.EvaluatedData.Attribute == GetHealthAttribute())
   {
      UE_LOG(LogTemp , Warning , TEXT("Health from GetHealth :%f"), GetHealth());
      UE_LOG(LogTemp , Warning , TEXT("Magnitude :%f"), Data.EvaluatedData.Magnitude);
   }*/
}
 
void UAuraAttributeSet::PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data)
{
   Super::PostGameplayEffectExecute(Data);
   FEffectProperties Props;
   SetEffectProperties(Data,Props);
}

void UAuraAttributeSet::OnRep_Health(const FGameplayAttributeData& OldHealth) const
{
   //这是一个宏,当对象在服务端发生变化的时候可以复制到所有客户端上
   GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,Health,OldHealth);
}

void UAuraAttributeSet::OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const
{
   GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,MaxHealth,OldMaxHealth);
}

void UAuraAttributeSet::OnRep_Mana(const FGameplayAttributeData& OldMana) const
{
   GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,Mana,OldMana);
}

void UAuraAttributeSet::OnRep_MaxMana(const FGameplayAttributeData& OldMaxMana) const
{
   GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,MaxMana,OldMaxMana);
}


      

附录

Ctrl + h 指定区域内替换关键字

相关推荐
yangmu32039 分钟前
《黎明行者之血》MOD整合包保姆级安装与使用指南
游戏·游戏程序
Jgch227 小时前
出海Facebook广告账户怎么选?
游戏·facebook
长脖鹿Johnny10 小时前
游戏输入系统框架设计(三):网络输入权威与可验证性
网络·游戏·游戏开发·架构设计·输入系统·网络同步
谈资一本本11 小时前
电脑全能修复大师 Gilisoft Total Repair,智能检测修复系统、应用、游戏问题!DLL、DirectX、.NET运行库损坏修复+缺失自动补齐!
游戏·电脑·.net
程序员-Benothing19 小时前
H3 Max开放世界RPG:AI原生游戏,从“能生成“到“能玩“差了多远
游戏·ai-native
CoderYanger1 天前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
无别0521 天前
【做游戏】做完游戏Demo后没地方发布?
游戏·玩游戏
lilian2331 天前
HarmonyOS 7 新特性(二十九)|游戏伴随服务:悬浮协作与质量降级
游戏·语音识别·harmonyos
人民广场吃泡面1 天前
DLSS 5 深度解析:当 AI 学会“创造”画面,实时游戏渲染迎来“GPT 时刻”
人工智能·gpt·游戏
学习星球1 天前
AI 一句话生成 3D 游戏世界:腾讯 HY-World 2.0 开源深度解析与本地实战
开发语言·人工智能·游戏·3d·ai·课程设计·ai编程