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


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

    AuraAttributeSet.h

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "AttributeSet.h"
    5. #include "AbilitySystemComponent.h"
    6. #include "GameFramework/Character.h"
    7. #include "AuraAttributeSet.generated.h"
    8. #define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
    9. GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
    10. GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
    11. GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
    12. GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)
    13. /**
    14. *
    15. */
    16. //这里是一个结构体
    17. USTRUCT()
    18. struct FEffectProperties
    19. {
    20. GENERATED_BODY()
    21. FEffectProperties(){}
    22. FGameplayEffectContextHandle EffectContextHandle;
    23. UPROPERTY()
    24. UAbilitySystemComponent* SourceASC = nullptr;
    25. UPROPERTY()
    26. AActor* SourceAvatarActor = nullptr;
    27. UPROPERTY()
    28. AController* SourceController = nullptr;
    29. UPROPERTY()
    30. ACharacter* SourceCharacter = nullptr;
    31. UPROPERTY()
    32. UAbilitySystemComponent* TargetASC = nullptr;
    33. UPROPERTY()
    34. AActor* TargetAvatarActor = nullptr;
    35. UPROPERTY()
    36. AController* TargetController = nullptr;
    37. UPROPERTY()
    38. ACharacter* TargetCharacter = nullptr;
    39. };
    40. UCLASS()
    41. class MYGAS_API UAuraAttributeSet : public UAttributeSet
    42. {
    43. GENERATED_BODY()
    44. public:
    45. UAuraAttributeSet();
    46. virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override;
    47. virtual void PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue) override;
    48. virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data) override;
    49. //创建AttributeData属性,并且是可以被复制的
    50. UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_Health ,Category="Vital Attributes")
    51. FGameplayAttributeData Health;
    52. ATTRIBUTE_ACCESSORS(UAuraAttributeSet,Health);
    53. UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_MaxHealth ,Category="Vital Attributes")
    54. FGameplayAttributeData MaxHealth;
    55. ATTRIBUTE_ACCESSORS(UAuraAttributeSet,MaxHealth);
    56. UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_Mana ,Category="Vital Attributes")
    57. FGameplayAttributeData Mana;
    58. ATTRIBUTE_ACCESSORS(UAuraAttributeSet,Mana);
    59. UPROPERTY(BlueprintReadOnly,ReplicatedUsing = OnRep_MaxMana ,Category="Vital Attributes")
    60. FGameplayAttributeData MaxMana;
    61. ATTRIBUTE_ACCESSORS(UAuraAttributeSet,MaxMana);
    62. UFUNCTION()
    63. void OnRep_Health(const FGameplayAttributeData& OldHealth) const;
    64. UFUNCTION()
    65. void OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const;
    66. UFUNCTION()
    67. void OnRep_Mana(const FGameplayAttributeData& OldMana) const;
    68. UFUNCTION()
    69. void OnRep_MaxMana(const FGameplayAttributeData& OldMaxMana) const;
    70. private:
    71. void SetEffectProperties(const FGameplayEffectModCallbackData& Data , FEffectProperties& Props) const;
    72. };

    AuraAttributeSet.cpp

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "AbilitySystem/AuraAttributeSet.h"
    3. #include "AbilitySystemBlueprintLibrary.h"
    4. #include "GameplayEffectExtension.h"
    5. #include "GameFramework/Character.h"
    6. #include "GameFramework/Pawn.h"
    7. #include "Net/UnrealNetwork.h"
    8. #include "PlayerController/AuraPlayerController.h"
    9. UAuraAttributeSet::UAuraAttributeSet()
    10. {
    11. InitHealth(50.f);
    12. InitMaxHealth(100.f);
    13. InitMana(25.f);
    14. InitMaxMana(50.f);
    15. }
    16. void UAuraAttributeSet::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const
    17. {
    18. //用来表示在Actor生命周期内要复制的属性,被创建并且复制的时候在网络上进行同步,确保客户端和服务端状态一致
    19. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
    20. //这是一个宏,用来表现属性的复制条件和什么时候调用RepNotify,其中Cond指属性什么时候进行复制,Repnotify是指什么时候调用RepNotify函数,这里是Always,只要服务器接收到属性值就调用复制
    21. DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,Health,COND_None,REPNOTIFY_Always);
    22. DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,MaxHealth,COND_None,REPNOTIFY_Always);
    23. // DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,Mana,COND_None,REPNOTIFY_Always);
    24. // DOREPLIFETIME_CONDITION_NOTIFY(UAuraAttributeSet,MaxMana,COND_None,REPNOTIFY_Always);
    25. }
    26. //数值在应用到角色前进行修改
    27. void UAuraAttributeSet::PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue)
    28. {
    29. Super::PreAttributeChange(Attribute, NewValue);
    30. //限制 health和Mana的最低值和最高值
    31. if(Attribute == GetHealthAttribute())
    32. {
    33. NewValue = FMath::Clamp(NewValue,0.f,GetMaxHealth());
    34. }
    35. if(Attribute == GetManaAttribute())
    36. {
    37. NewValue = FMath::Clamp(NewValue,0.f,GetMaxMana());
    38. }
    39. }
    40. void UAuraAttributeSet::SetEffectProperties(const FGameplayEffectModCallbackData& Data, FEffectProperties& Props) const
    41. {
    42. // Source = causer of the effect , Targetg = Target of the Effect (owner of this AS) 效果的上下文处理,包括来源和目标
    43. Props.EffectContextHandle = Data.EffectSpec.GetContext();
    44. // 获取效果的来源能力系统组件
    45. Props.SourceASC =Props.EffectContextHandle.GetOriginalInstigatorAbilitySystemComponent();
    46. // 如果有效,则获取来源的相关信息
    47. if(IsValid(Props.SourceASC) && Props.SourceASC->AbilityActorInfo.IsValid() && Props.SourceASC->AbilityActorInfo->AvatarActor.IsValid())
    48. {
    49. Props.SourceAvatarActor = Props.SourceASC->AbilityActorInfo->AvatarActor.Get();
    50. Props.SourceController = Props.SourceASC->AbilityActorInfo->PlayerController.Get();
    51. // 如果来源控制器为空且存在角色,则获取角色的控制器
    52. if(Props.SourceController == nullptr && Props.SourceAvatarActor != nullptr)
    53. {
    54. if(const APawn* Pawn = Cast(Props.SourceAvatarActor))
    55. {
    56. Props.SourceController = Pawn->GetController();
    57. }
    58. }
    59. // 如果存在来源控制器,则尝试将其转换为角色
    60. if(Props.SourceController)
    61. {
    62. Props.SourceCharacter = Cast(Props.SourceController->GetPawn());
    63. }
    64. }
    65. // 获取目标的相关信息
    66. if(Data.Target.AbilityActorInfo.IsValid() && Data.Target.AbilityActorInfo->AvatarActor.IsValid())
    67. {
    68. Props.TargetAvatarActor = Data.Target.AbilityActorInfo->AvatarActor.Get();
    69. Props.TargetController = Data.Target.AbilityActorInfo->PlayerController.Get();
    70. Props.TargetCharacter = Cast(Props.TargetAvatarActor);
    71. Props.TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(Props.TargetAvatarActor);
    72. }
    73. //这里是用来Debug对生命值产生的数值
    74. /*if(Data.EvaluatedData.Attribute == GetHealthAttribute())
    75. {
    76. UE_LOG(LogTemp , Warning , TEXT("Health from GetHealth :%f"), GetHealth());
    77. UE_LOG(LogTemp , Warning , TEXT("Magnitude :%f"), Data.EvaluatedData.Magnitude);
    78. }*/
    79. }
    80. void UAuraAttributeSet::PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data)
    81. {
    82. Super::PostGameplayEffectExecute(Data);
    83. FEffectProperties Props;
    84. SetEffectProperties(Data,Props);
    85. }
    86. void UAuraAttributeSet::OnRep_Health(const FGameplayAttributeData& OldHealth) const
    87. {
    88. //这是一个宏,当对象在服务端发生变化的时候可以复制到所有客户端上
    89. GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,Health,OldHealth);
    90. }
    91. void UAuraAttributeSet::OnRep_MaxHealth(const FGameplayAttributeData& OldMaxHealth) const
    92. {
    93. GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,MaxHealth,OldMaxHealth);
    94. }
    95. void UAuraAttributeSet::OnRep_Mana(const FGameplayAttributeData& OldMana) const
    96. {
    97. GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,Mana,OldMana);
    98. }
    99. void UAuraAttributeSet::OnRep_MaxMana(const FGameplayAttributeData& OldMaxMana) const
    100. {
    101. GAMEPLAYATTRIBUTE_REPNOTIFY(UAuraAttributeSet,MaxMana,OldMaxMana);
    102. }

    附录

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

  • 相关阅读:
    list(stl)
    滴灌通运营模式遭质疑:是P2P,是套利,还是模式创新?
    基于随机森林实现特征选择降维及回归预测(Matlab代码实现)
    定了,JDK 19 功能集冻结,JDK官方终于支持支持协程了
    sqlserver2012 bat脚本实现最大使用内存设置
    Hadoop高手之路3-Hadoop集群搭建
    网络安全运维工程师(NISP-SO)需要掌握那些知识点
    一篇文章搞懂WPF动画的使用技巧
    线性代数矩阵相关知识回顾
    MySql对于时间段交集的处理和通用实现方式(MyBatis-Plus)
  • 原文地址:https://blog.csdn.net/weixin_42666952/article/details/138183230