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

news/2024/10/20 0:27:38/

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

AuraAttributeSet.h

// 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

// 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 指定区域内替换关键字


http://www.ppmy.cn/news/1442668.html

相关文章

Docker搭建MySQL Workbench

MySQL Workbench 是一款图形界面工具&#xff0c;用于数据库设计、开发、管理和维护MySQL、MariaDB和Percona Server数据库。它提供了SQL开发、数据库设计、服务器配置等功能。 实际应用场景 数据库设计&#xff1a;创建ER模型&#xff0c;生成数据库架构。SQL开发&#xff1…

接口的构成

目录 接口 一、URL 二、请求方法 三、请求报文&#xff08;request message&#xff09; 3.1请求行 3.2请求头 3.3 请求体 四、响应报文&#xff08;response message&#xff09; 4.1响应行 4.2响应头 4.3响应体 接口 接口就是API&#xff0c;是程序开发的函数和方…

[97编程世界冠军4K组]代码被转为ts脚本github代码如何在WIN10运行和调试

源代码地址&#xff1a;iGitHub - SuperSodaSea/Omniscent: Analysis and Replication of Omniscent, the 1st in the Mekka & Symposium 1997 PC 4K Intro Competition. 1、软件安装nodejs和webstorm都要安装&#xff1a; node-v20.12.2-x64.msi WebStorm-2024.1.1.exe 代…

文件上传复习(upload-labs14-17关)

因为14-16关需要用到图片码&#xff0c;所以了解一下图片码 利用copy命令合成一个图片马使用_copy 图片马-CSDN博客 图片木马制作方法详细教程_如何制作图片马-CSDN博客 Pass-14&#xff08;文件包含图片马&#xff09; 首先&#xff0c;图片的格式在防护中通常是不会使用后…

应用实战 | 别踩白块小游戏,邀请大家来PK挑战~

“踩白块会输”是一个简单的微信小程序游戏&#xff0c;灵感来自当年火热的别踩白块游戏&#xff0c;程序内分成三个模块&#xff1a;手残模式、经典模式和极速模式&#xff0c;分别对应由易到难的三种玩法&#xff0c;可以查看游戏排名。动画效果采用JS实现&#xff0c;小程序…

【真实体验】使用崖山YMP 迁移 Oracle/MySQL 至YashanDB 23.2 验证测试【YashanDB迁移体验官】

一、前言 说一下我和崖山数据库的结缘&#xff0c;大概在去年吧&#xff0c;因为我经常在墨天轮写文章&#xff0c;看到崖山数据库推出了一崖山体验官的活动&#xff0c;我就报名参加了。第一次体验了崖山数据库&#xff0c;也测试了我司数据库到崖山数据库的兼容性&#xff0…

【Java Spring MVC项目异常解决】HTTP 404

报404错误多数情况下是因为路径问题&#xff0c;特别是在基于MVC框架的Web应用中。HTTP 404错误是客户端错误响应代码&#xff0c;表明服务器无法找到客户端请求的资源&#xff08;例如&#xff0c;Web页面、图片、文件&#xff09;。在Spring MVC项目中&#xff0c;404错误常见…

electron 中的 ipcMain 介绍

在 Electron 应用中&#xff0c;ipcMain 是主进程中的一个模块&#xff0c;用于处理与渲染进程之间的进程间通信 (IPC, Inter-Process Communication)。Electron 应用程序通常分为两个主要的进程&#xff1a;主进程和渲染进程。主进程&#xff08;通常是 main.js 文件&#xff…