开启插件

加入模块

		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore",

      
            "GameplayAbilities",
            "GameplayTags",
            "GameplayTasks",


新建属性类

C++继承

 头文件

#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "MyAttributeSet.generated.h"

/**
 * 
 */
UCLASS()
class PROJECT_C_API UMyAttributeSet : public UAttributeSet
{
	GENERATED_BODY()
		UMyAttributeSet();
public:

	//重写函数
	virtual void PostGameplayEffectExecute(const struct FGameplayEffectModCallbackData& Data) override;


	//定义生命值 类型是预定义的
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FGameplayAttributeData Life;
	//定义最大生命值
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FGameplayAttributeData MaxLife;

};

源文件


#include "MyAttributeSet.h"
#include "GameplayEffectExtension.h"
#include "GameplayEffect.h"


UMyAttributeSet::UMyAttributeSet() :
	Life(100.f), MaxLife(100.f)
{

}

void UMyAttributeSet::PostGameplayEffectExecute(const struct FGameplayEffectModCallbackData& Data)
{
	Super::PostGameplayEffectExecute(Data);


	UProperty* updateProperty = Data.EvaluatedData.Attribute.GetUProperty();
	UProperty* checkProperty = FindFieldChecked<UProperty>(UMyAttributeSet::StaticClass(), GET_MEMBER_NAME_CHECKED(UMyAttributeSet, Life));
	if (updateProperty == checkProperty)
	{
		Life.SetCurrentValue(FMath::Clamp<float>(Life.GetCurrentValue(), 0, MaxLife.GetCurrentValue()));
	}
}

character头文件

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
//GAS接口头文件
#include "AbilitySystemInterface.h"
#include "GASCharacter.generated.h"

class UAbilitySystemComponent;
class UGameplayAbility;
class UMyAttributeSet;

UCLASS()
class PROJECT_C_API AGASCharacter : public ACharacter, public IAbilitySystemInterface
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AGASCharacter();

	/GAS  Star
public:
	//定义组件
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UAbilitySystemComponent* AbilitySystemComp;
    
    	//定义属性引用
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UMyAttributeSet* MyAttributeSet;


public:

	//获取某种技能
	UFUNCTION(BlueprintCallable)
		void GetAbility(TSubclassOf<UGameplayAbility> AblityType);

	//获取技能组件
	virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;


	GAS End

函数实现

#include "GASCharacter.h"
#include "AbilitySystemComponent.h"
#include "MyAttributeSet.h"
#include "Abilities/GameplayAbility.h"

// Sets default values
AGASCharacter::AGASCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	GAS

	AbilitySystemComp = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("AbilityStytemComp"));
	//定义类型
	MyAttributeSet = CreateDefaultSubobject<UMyAttributeSet>(TEXT("MyAttributeSet"));

	//GAS End
}

//GAS  获取能力
void AGASCharacter::GetAbility(TSubclassOf<UGameplayAbility> AblityType)
{
	if (AbilitySystemComp && AblityType)
	{
		//从组件拿到能力
		AbilitySystemComp->GiveAbility(FGameplayAbilitySpec(AblityType));
		//组件初始化输入
		AbilitySystemComp->InitAbilityActorInfo(this, this);
	}
}

//GAS  获取组件引用
UAbilitySystemComponent* AGASCharacter::GetAbilitySystemComponent() const
{
	return AbilitySystemComp;
}

定义技能

蓝图继承或C++继承都可以

 技能是载体,响应其它类发生的事情。最重要的是在这个蓝图中播放蒙太奇动画。

使用技能

在chanacter中使用技能

 在技能类中释放技能,并执行伤害

 定义CD

事件

 

 

 触发事件

定义伤害

攻防公式

文章参考:【虚幻4】【UE4】Gameplay Ability System系统完整学习_哔哩哔哩_bilibili