当前位置: 首页 > news >正文

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 Interfacevirtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;//End IAbilitySystemInterface Interfaceprotected://创建能力组件UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "AbilitySystem")UXMBAbilitySystemComponent* XMBAbilitySystemComponent;//创建属性集UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "AbilitySystem")UXMBAttributeSet* XMBAttributeSet;//Begin Apawn Interfacevirtual 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 Interfacevirtual 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 Interfacevirtual 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类,

相关文章:

  • Linux 的 epoll 与 Windows 的 IOCP 详解
  • 重塑数学边界:人工智能如何引领数学研究的新纪元
  • Transformer架构:基于自注意力机制推动NLP革命性突破
  • 有机玻璃材质数据采集活性炭吸附气体中二氧化硫实验装置
  • 别样健康养生之道
  • 【Arthas】火焰图优化应用CPU(问题原因:获取调用栈)
  • sonar-scanner在扫描JAVA项目时为什么需要感知.class文件
  • FPGA DDR4多通道管理控制器设计
  • 影楼精修-露齿笑算法解析
  • Python Cookbook-6.18 用__init__参数自动初始化实例变量
  • Pillow 玩图术:轻松获取图片尺寸和颜色模式
  • python进阶(1)字符串
  • Vue中的过滤器知道多少?从是什么、怎么用、应用场景、原理分析、示例解释
  • luaopen系列标准库使用解析
  • 生成式 AI 与 AI 的区别
  • 第12章:精神力的禁忌边界
  • 办公文档全能处理工具功能解析
  • C语言奇幻指南:宏、头文件与变量的秘密世界
  • Java 中如何实现自定义类加载器,应用场景是什么?
  • python中的异常处理
  • 做网站栏目是什么意思/百度推广时间段在哪里设置
  • 招聘网站建设技术要求/搜索引擎外部链接优化
  • 梅州建站联系方式/优化疫情防控
  • 网站群如何做网站/今天刚刚发生的新闻
  • 平度市网站建设/腾讯网网站网址
  • 网站建设 广西/百度seo效果