这里以第三人称模板(C++版)为基础进行开发。
并且启用GAS插件:
勾选插件后,关闭引擎编辑器。
注意: 默认要求重启,这里直接关闭,待进行完后续代码编辑操作后通过编译器启动项目。
在引擎内启用插件只是允许在编辑器内创建基本的GA、GE,但只是这样并无法对其进行使用,而且在代码层面的使用也需要直接进入到代码中对相应的模块进行依赖性的添加。
在项目的build.cs
文件中(项目名.build.cs
),添加模块的引用:
PrivateDependencyModuleNames.AddRange(new string[]
{
"GameplayAbilities",
"GameplayTasks",
"GameplayTags"
});
创建角色基类GASCharacterBase
(完成GAS组件在服务器的初始化):
#pragma once
#include "CoreMinimal.h"
#include "AbilitySystemInterface.h"
#include "GameFramework/Character.h"
#include "GASCharacterBase.generated.h"
UCLASS()
class INSIDEGAS_API AGASCharacterBase : public ACharacter, public IAbilitySystemInterface
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AGASCharacterBase();
virtual void PossessedBy(AController* NewController) override;
public:
FORCEINLINE virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override
{
return AbilitySystemComponent;
}
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UAbilitySystemComponent* AbilitySystemComponent;
static FName AbilitySystemComponentName;
};
#include "GASCharacterBase.h"
#include "AbilitySystemComponent.h"
FName AGASCharacterBase::AbilitySystemComponentName(TEXT("AbilitySystemComponent"));
// Sets default values
AGASCharacterBase::AGASCharacterBase()
{
// 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;
AbilitySystemComponent = CreateDefaultSubobject(
AGASCharacterBase::AbilitySystemComponentName);
}
void AGASCharacterBase::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
if (IsValid(AbilitySystemComponent))
{
AbilitySystemComponent->InitAbilityActorInfo(this, this);
}
SetOwner(NewController);
}
创建控制器基类GASPlayerControllerBase
(完成GAS组件在客户端的初始化,单机游戏可以忽略这里的操作):
#pragma once
#include "CoreMinimal.h"
#include "GASPlayerControllerBase.generated.h"
UCLASS()
class INSIDEGAS_API AGASPlayerControllerBase : public APlayerController
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGASPlayerControllerBase();
virtual void AcknowledgePossession(APawn* P) override;
};
#include "GASPlayerControllerBase.h"
#include "AbilitySystemComponent.h"
#include "GASCharacterBase.h"
// Sets default values
AGASPlayerControllerBase::AGASPlayerControllerBase()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
void AGASPlayerControllerBase::AcknowledgePossession(APawn* P)
{
Super::AcknowledgePossession(P);
AGASCharacterBase* PossessedCharacter = Cast(P);
if (IsValid(PossessedCharacter))
{
PossessedCharacter->GetAbilitySystemComponent()->InitAbilityActorInfo(PossessedCharacter, PossessedCharacter);
}
}
将系统创建的角色类的基类(主要包含移动,输入,还有相机相关的代码,后续这部分也需要改造,这里暂时使用默认的代码),修改为我们创建的角色类GASCharacterBase
,如:
class AInsideGASCharacter : public AGASCharacterBase
完成后,编译并启动项目。
在content里创建GA的蓝图GA_FirstAbility
:
在其中构建简单的测试逻辑:
此外,设置技能的Net Execution Policy
为Server Initiated
(默认为Local Predicted
)。
在第三人称蓝图中进行技能的赋予,以及技能通过输入的触发:
运行并按下T键进行测试,结果: