GameAbilitySystem入门记录(一)
目录
0、建议准备工作
①下载Github上的GASDocumentation(UE 4.26.2)或UE商场中自带的ActionRPG项目(图一的两个项目)
②下载中文版文档
1、启用GAS
1.1 新建项目
本文主要用以学习记录。版本使用的是4.26.2。项目时间是2022.7.21。
1.2 启用GAS插件
1.3 验证GAS有效性
此处存有GameplayCue Editor的话说明启用成功
此处存在Gameplay Ability Blueprint的话,也说明已经启用成功
1.4 声明GAS项目
由于我们创建的是C++的UE项目
1.4.1 打开 xxxx.Build.cs
添加上述 3个 完成项目的GAS声明
2、能“HelloWorld”的Ability
万事皆有HelloWorld,GAS也不例外。
2.1 创建“能够HelloWorld”的Ability
在内容浏览器中,新建一个GA BP,命名为GA_testHello。
双击进入,添加一个PrintString作为Helloworld的动作
(此处print的是一个“Action”)
3、为Character启用ASC
GAS的最终目的就是让玩家发出技能,而玩家控制的便是——Character。因此需要为Character添加ASC(Ability System Component)。
3.1 从Character.h和Character.cpp入手
因为我们在1.1创建项目的时候已经选择了带有初学者包,也就是说项目创建之初便带有一个默认的Character。
打开VS项目,右侧的资源管理器就记载了我们的代码。其中,项目名+Character.h
和项目名+Character.cpp
便是我们要添加ASC的Character。
3.2 修改Character.h
打开.h文件后,需要更改几处代码:
①头文件部分
#include "AbilitySystemInterface.h"
#include "AbilitySystemComponent.h"
注意,#include "learnUe_0720Character.generated.h"
这个头文件必须在最后一个引用,否则会报错:
添加如下即可:
②添加IAbilitySystemInterface
接口
在 .h文件的第一个Public中添加如下代码 声明ASC :
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = GameplayAbilities,meta = (AllowPrivateAccess = "true"))
class UAbilitySystemComponent* AbilitySystem;
UAbilitySystemComponent* GetAbilitySystemComponent()const override;
即如下图所示
添加完 .h文件,下一步当然是添加 .cpp文件
3.3 修改Character.cpp
①在文件的末尾添加如下代码:
UAbilitySystemComponent* AlearnUe_0720Character::GetAbilitySystemComponent() const
{
return AbilitySystem;
}
该函数是用来实现 h文件中3.2最后添加的UAbilitySystemComponent* GetAbilitySystemComponent()const override;
此时,可以回到UE项目中编译一下,如果编译通过,那么可以继续。
如果编译成功,可以看到我们的Character上面多了ASC:
4、代码备份
4.1 Character.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
//添加引用
#include "AbilitySystemInterface.h"
#include "AbilitySystemComponent.h"
//最后引用
#include "learnUe_0720Character.generated.h"
UCLASS(config=Game)
class AlearnUe_0720Character : public ACharacter , public IAbilitySystemInterface //修改:添加接口
{
GENERATED_BODY()
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
public:
AlearnUe_0720Character();
//modi: 声明ASC 设置为Private后蓝图是无法读取的,因此需要meta设置为true才可以被蓝图看到
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = GameplayAbilities,meta = (AllowPrivateAccess = "true"))
class UAbilitySystemComponent* AbilitySystem;
//modi:添加一个返回此组件的方法
UAbilitySystemComponent* GetAbilitySystemComponent()const override;
/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseTurnRate;
/** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseLookUpRate;
protected:
/** Resets HMD orientation in VR. */
void OnResetVR();
/** Called for forwards/backward input */
void MoveForward(float Value);
/** Called for side to side input */
void MoveRight(float Value);
/**
* Called via input to turn at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void TurnAtRate(float Rate);
/**
* Called via input to turn look up/down at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void LookUpAtRate(float Rate);
/** Handler for when a touch input begins. */
void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);
/** Handler for when a touch input stops. */
void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);
protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// End of APawn interface
//TWeakObjectPtr<class UGDAbilitySystemComponent> AbilitySystemComponent;
public:
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const {
return CameraBoom; }
/** Returns FollowCamera subobject **/
FORCEINLINE class UCameraComponent* GetFollowCamera() const {
return FollowCamera; }
};
4.2 Character.cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "learnUe_0720Character.h"
#include "HeadMountedDisplayFunctionLibrary.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
//
// AlearnUe_0720Character
AlearnUe_0720Character::AlearnUe_0720Character()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// set our turn rates for input
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
/