RPG7.准备GAS的工作

1.启动项目,为项目添加gameplayability插件

2.添加abilitysystemcomponent的c++类

3.添加attributeset的c++类

4.往build.cs内添加模块

5.进入CharacterBase内,添加gameplayasystem和attributbeset,覆写PossessedBy()和GetAbilitysystemcomponent()

复制代码
class ARPG_GRIVITY_API ACharacterBase : public ACharacter, public IAbilitySystemInterface
{
	GENERATED_BODY()

public:
	ACharacterBase();

	//创建能力组件的获取方法
	UXMBAbilitySystemComponent* GetXMBAbilitySystemComponent() const {return  XMBAbilitySystemComponent;}
	
	//创建属性集的获取方法
	UXMBAttributeSet* GetXMBAttributeSet() const {return  XMBAttributeSet;}

	//Begin IAbilitySystemInterface Interface
	virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;
	//End IAbilitySystemInterface Interface

	
protected:
	//创建能力组件
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "AbilitySystem")
	UXMBAbilitySystemComponent* XMBAbilitySystemComponent;
	
	//创建属性集
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "AbilitySystem")
	UXMBAttributeSet* XMBAttributeSet;

	//Begin Apawn Interface
	virtual void PossessedBy(AController* NewController) override;
	//End Apawn Interface
};

6.在构造函数里创建,覆写函数体内

复制代码
#include "Character/CharacterBase.h"
#include "AbilitySystem/XMBAttributeSet.h"
#include "AbilitySystem/XMBAbilitySystemComponent.h"

ACharacterBase::ACharacterBase()
{
	PrimaryActorTick.bCanEverTick = false;
	//初始禁用 Tick,适合需要手动控制更新的场景。
	PrimaryActorTick.bStartWithTickEnabled = false;

	//禁用贴花接收,适用于性能优化或特定视觉需求。
	GetMesh()->bReceivesDecals = false;

	XMBAbilitySystemComponent = CreateDefaultSubobject<UXMBAbilitySystemComponent>(TEXT("XMBAbilitySystemComponent"));

	XMBAttributeSet = CreateDefaultSubobject<UXMBAttributeSet>(TEXT("XMBAttributeSet"));
}

UAbilitySystemComponent* ACharacterBase::GetAbilitySystemComponent() const
{
	return GetXMBAbilitySystemComponent();
}

void ACharacterBase::PossessedBy(AController* NewController)
{
	Super::PossessedBy(NewController);

	if (XMBAbilitySystemComponent)
	{
		XMBAbilitySystemComponent->InitAbilityActorInfo(this, this);
	}
	
}

7.进入到玩家控制的角色内(XMBCharacter),对PossessedBy覆写

复制代码
//Begin Apawn Interface
	virtual void PossessedBy(AController* NewController) override;
	//End Apawn Interface

void AXMBCharacter::PossessedBy(AController* NewController)
{
	Super::PossessedBy(NewController);

	if (XMBAbilitySystemComponent && XMBAttributeSet)
	{
		GEngine->AddOnScreenDebugMessage(-1,5.f, FColor::Red, FString::Printf(TEXT("Owner: %s, Avatar : %s"),*XMBAbilitySystemComponent->GetOwnerActor()->GetActorLabel(),*XMBAbilitySystemComponent->GetAvatarActor()->GetActorLabel()));
	}
}

8.启动,能从日志里看到信息

9.我们需要给AbilitySystemComponent赋予Ability,当玩家尝试激活Ability时,实际上是从ASC中激活能力。对于激活的这一部分,我们可以自定义Ability的激活方式。

继续创建一个GameplayAbility的c++类,然后在其内创建一个激活策略的枚举,覆写两个函数

复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Abilities/GameplayAbility.h"
#include "XMBGameplayAbility.generated.h"

//创建一个激活能力的策略枚举
UENUM()
enum class EXMBAbilityActivationPolicy : uint8
{
	OnTriggered,
	OnGiven
};
/**
 * 
 */
UCLASS()
class ARPG_GRIVITY_API UXMBGameplayAbility : public UGameplayAbility
{
	GENERATED_BODY()

public:
	 
	
protected:
	//设置激活策略
	UPROPERTY(EditDefaultsOnly, Category = "XMBAbility")
	EXMBAbilityActivationPolicy AbilityActivationPolicy = EXMBAbilityActivationPolicy::OnTriggered;

	//~ Begin UGameplayAbility Interface
	virtual void OnGiveAbility(const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilitySpec& Spec) override;
	virtual void EndAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, bool bReplicateEndAbility, bool bWasCancelled) override;
	//~ End UGameplayAbility Interface
	
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "AbilitySystem/Abilities/XMBGameplayAbility.h"

#include "AbilitySystem/XMBAbilitySystemComponent.h"

//在Ability被分配给AbilitySystemComponent后立即调用
void UXMBGameplayAbility::OnGiveAbility(const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilitySpec& Spec)
{
	Super::OnGiveAbility(ActorInfo, Spec);

	if (AbilityActivationPolicy == EXMBAbilityActivationPolicy::OnGiven)
	{
		if (ActorInfo && !Spec.IsActive())
		{
			//Spec.Handle是FGameplayAbilitySpec对象的句柄,表示一个具体的能力实例
			ActorInfo->AbilitySystemComponent->TryActivateAbility(Spec.Handle);
		}
	}
}

void UXMBGameplayAbility::EndAbility(const FGameplayAbilitySpecHandle Handle,
	const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo,
	bool bReplicateEndAbility, bool bWasCancelled)
{
	Super::EndAbility(Handle, ActorInfo, ActivationInfo, bReplicateEndAbility, bWasCancelled);

	if (AbilityActivationPolicy == EXMBAbilityActivationPolicy::OnGiven)
	{
		if (ActorInfo)
		{
			//Handle是FGameplayAbilitySpec对象的句柄,表示要清除的能力实例。
			ActorInfo->AbilitySystemComponent->ClearAbility(Handle);
		}
	}
}

10.启动引擎,右键即可创建一个刚才自己创建的gameplayability类,

相关推荐
2601_963869951 分钟前
【计算机毕业设计】基于Java的潮牌购物网站系统的设计与实现
java·开发语言·课程设计
203号居民8 分钟前
LeetCode hot 100 —560. 和为 K 的子数组
java·算法·leetcode
名字还没想好☜26 分钟前
Go 的 io.Reader/Writer 组合实战:io.Copy、TeeReader、MultiWriter 优雅处理数据流
开发语言·后端·golang·go·iphone
浪客川27 分钟前
使用 IDEA 生成API文档
java·ide·intellij-idea
郝学胜-神的一滴38 分钟前
Qt 高级编程 042:进度条从入门到自定义美化
开发语言·c++·qt·软件开发·用户界面
鸿芯微控科技1 小时前
压电纳米定位分辨率怎么测?噪声、带宽、最小可检测位移与Python分析
开发语言·python·噪声分析·压电执行器·纳米定位·位移测量
上下求索,莫负韶华1 小时前
笔记-数据库事务和java事务和切面
java·数据库·笔记
@小匠1 小时前
Spring Boot Nacos绑定 Map 时中文 key 导致启动失败:一次从复现到源码的排查实录
java·开发语言·前端
Knight_AL1 小时前
Lombok @Builder 踩坑:build() 前后对象类型不一样
android·java·开发语言
神明不懂浪漫1 小时前
【第五章】队列
开发语言·数据结构·经验分享·笔记·算法