一、为Actor添加AbilitySystemComponent组件(这里以Character为例)
1、在VS里打开自定义Character脚本,引入头文件,使其继承IAbilitySystemInterface接口。
#include "AbilitySystemInterface.h"
class GASINTRO_API ATestCharacter : public ACharacter, public IAbilitySystemInterface
2、实现接口的GetAbilitySystemComponent方法,并声明其返回值类型的变量
TestCharacter.h:
public:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "BaseCharacter")
class UAbilitySystemComponent* AbilitySystemComponent;
public:
virtual UAbilitySystemComponent* GetAbilitySystemComponent() const;
TestCharacter.cpp:
#include "AbilitySystemComponent.h"
ATestCharacter::ATestCharacter()
{
PrimaryActorTick.bCanEverTick = true;
AbilitySystemComponent = CreateDefaultSubobject<UAbilitySystemComponent>("AbilitySystemComponent");
}
UAbilitySystemComponent* ATestCharacter::GetAbilitySystemComponent() const
{
return AbilitySystemComponent;
}
二、通过AquireAbility让角色学会能力
TestCharacter.h:
public:
UFUNCTION(BlueprintCallable, Category = "BaseCharacter")
void AquireAbility(TSubclassOf<UGameplayAbility> Ability);
注:FGameplayAbilitySpec其实是个结构体,可以查看其源码发现有很多初始化构造函数,包括能力等级等等
TestCharacter.cpp:
void ATestCharacter::AquireAbility(TSubclassOf<UGameplayAbility> Ability)
{
if (AbilitySystemComponent && Ability)
{
AbilitySystemComponent->GiveAbility(FGameplayAbilitySpec(Ability)); //使用FGameplayAbilitySpec实例化Ability
AbilitySystemComponent->InitAbilityActorInfo(this, this);//初始化能力信息,单机参数都是自己,如果跟网络相关,参数一个是Playerstadium,一个是Character类
}
}
编译完成后创建C++的蓝图类,会发现多出来个Ability System Component组件