【UE】GAS入门(一)创建Ability

GameAbilitySystem入门记录(一)

0、建议准备工作

①下载Github上的GASDocumentation(UE 4.26.2)或UE商场中自带的ActionRPG项目(图一的两个项目)
②下载中文版文档


1、启用GAS

1.1 新建项目

本文主要用以学习记录。版本使用的是4.26.2。项目时间是2022.7.21。

111
112
113

1.2 启用GAS插件

121
122

1.3 验证GAS有效性

132
此处存有GameplayCue Editor的话说明启用成功
131
此处存在Gameplay Ability Blueprint的话,也说明已经启用成功

1.4 声明GAS项目

由于我们创建的是C++的UE项目

1.4.1 打开 xxxx.Build.cs

1411
添加上述 3个 完成项目的GAS声明



2、能“HelloWorld”的Ability

万事皆有HelloWorld,GAS也不例外。

2.1 创建“能够HelloWorld”的Ability

211
214

在内容浏览器中,新建一个GA BP,命名为GA_testHello。
212
双击进入,添加一个PrintString作为Helloworld的动作
213
(此处print的是一个“Action”)

3、为Character启用ASC

GAS的最终目的就是让玩家发出技能,而玩家控制的便是——Character。因此需要为Character添加ASC(Ability System Component)。

3.1 从Character.h和Character.cpp入手

因为我们在1.1创建项目的时候已经选择了带有初学者包,也就是说项目创建之初便带有一个默认的Character。

311
打开VS项目,右侧的资源管理器就记载了我们的代码。其中,项目名+Character.h项目名+Character.cpp便是我们要添加ASC的Character。

3.2 修改Character.h

打开.h文件后,需要更改几处代码:
①头文件部分

#include "AbilitySystemInterface.h"
#include "AbilitySystemComponent.h"

注意,#include "learnUe_0720Character.generated.h"这个头文件必须在最后一个引用,否则会报错:
321
添加如下即可:
323


②添加IAbilitySystemInterface接口

322
在 .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"
#
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值