UE4 GAS 01 -基础环境搭建

本文介绍了如何在Unreal Engine 4中通过Gameplay Ability System (GAS)为角色类添加能力组件,并详细讲解了如何配置Character类以支持能力获取和使用。从插件设置到代码实现,为初学者提供了一个完整的基础教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学GAS,仅用作自己查缺补漏,不想再做时又去找视频
参考教程:
英文原版(联机):Ambience Lee:
https://www.udemy.com/course/introduction-to-unreal-engine-4-ability-system/
中文翻译重置(单机):小明:
https://space.bilibili.com/149146076/channel/detail?cid=127939&ctype=0

一、插件环境

1.1、在Editor中勾选GameplayAbility并关闭

在这里插入图片描述

1.2、在.Build.cs中添加对应模块

PrivateDependencyModuleNames.AddRange(new string[] { "GameplayAbilities","GameplayTags","GameplayTasks" });

二、Character类配置

2.1、添加UAbilitySystemCompnent

2.2、并继承IAbilitySystemInterface

2.3、重写GetAbilitySystemComponent()

2.4、写一个可以让蓝图获取能力的接口AquireAbility

.h:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "AbilitySystemInterface.h"
#include "MyGASCharacter.generated.h"

UCLASS()
class GASTEST_API AMyGASCharacter : public ACharacter,public IAbilitySystemInterface
{
	GENERATED_BODY()

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

	UFUNCTION(BlueprintPure)
	virtual UAbilitySystemComponent* GetAbilitySystemComponent() const;

	UFUNCTION(BlueprintCallable)
		void AquireAbility(TSubclassOf<class UGameplayAbility> InAbility);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
	UPROPERTY(VisibleAnywhere,BlueprintReadOnly)
	class UAbilitySystemComponent* AbilitySystemComp;
};

.cpp

#include "MyGASCharacter.h"
#include "AbilitySystemComponent.h"

// Sets default values
AMyGASCharacter::AMyGASCharacter()
{
 	// 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;

	AbilitySystemComp = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("AbilitySystemComp"));
	
}

UAbilitySystemComponent* AMyGASCharacter::GetAbilitySystemComponent() const
{
	return AbilitySystemComp;
}

void AMyGASCharacter::AquireAbility(TSubclassOf<class UGameplayAbility> InAbility)
{
	if (AbilitySystemComp)
	{
		if (HasAuthority()&&InAbility)
		{
			AbilitySystemComp->GiveAbility(FGameplayAbilitySpec(InAbility, 1,0));
			
		}
		AbilitySystemComp->InitAbilityActorInfo(this, this);
	}
}

// Called when the game starts or when spawned
void AMyGASCharacter::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AMyGASCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void AMyGASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

此时,我们已经拥有了一个可以简单使用能力系统的角色

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值