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

虚幻引擎5 GAS开发俯视角RPG游戏 P07-07 激活能力

目标:在输入回调函数里,激活能力

1.技能类添加输入标签:

// 版权归陈超所有#pragma once#include "CoreMinimal.h"
#include "Abilities/GameplayAbility.h"
#include "CC_GameplayAbility.generated.h"/*** */
UCLASS()
class CC_AURA_API UCC_GameplayAbility : public UGameplayAbility
{GENERATED_BODY()public:UPROPERTY(EditDefaultsOnly, Category= "CC|Input")FGameplayTag StartupInputTag;		//启动能力标签};

2.我们在玩家控制类中调用能力系统组件里的方法,来激活能力

(1)得到能力系统组件

Source/CC_Aura/Public/Player/CC_PlayerController.h

	UPROPERTY()TObjectPtr<UCC_AbilitySystemComponent> CC_AbilitySystemComponent;		//能力系统组件UCC_AbilitySystemComponent* GetCC_AbilitySystemComponent();

Source/CC_Aura/Private/Player/CC_PlayerController.cpp:

UCC_AbilitySystemComponent* ACC_PlayerController::GetCC_AbilitySystemComponent()
{if (CC_AbilitySystemComponent == nullptr){UAbilitySystemComponent* AbilitySystemComponent = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetPawn<APawn>());CC_AbilitySystemComponent = Cast<UCC_AbilitySystemComponent>(AbilitySystemComponent);}return CC_AbilitySystemComponent;
}

(2)激活能力:

Source/CC_Aura/Private/Player/CC_PlayerController.cpp:

/*开始按下*/
void ACC_PlayerController::AbilityInputTagPressed(FGameplayTag InputTag)
{// GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, *InputTag.ToString());
}/*释放*/
void ACC_PlayerController::AbilityInputTagReleased(FGameplayTag InputTag)
{if (GetCC_AbilitySystemComponent() == nullptr) return;GetCC_AbilitySystemComponent()->AbilityInputTagReleased(InputTag);
}/*保持*/
void ACC_PlayerController::AbilityInputTagHold(FGameplayTag InputTag)
{if (GetCC_AbilitySystemComponent() == nullptr) return;GetCC_AbilitySystemComponent()->AbilityInputTagHeld(InputTag);
}

3.在能力系统组件里创建方法:

Source/CC_Aura/Public/AbilitySystem/CC_AbilitySystemComponent.h:

	void AbilityInputTagHeld(const FGameplayTag& InputTag);			//按键保持void AbilityInputTagReleased(const FGameplayTag& InputTag);		//按键释放

方法实现:

Source/CC_Aura/Private/AbilitySystem/CC_AbilitySystemComponent.cpp:

void UCC_AbilitySystemComponent::AbilityInputTagHeld(const FGameplayTag& InputTag)
{if (!InputTag.IsValid()) return;for (FGameplayAbilitySpec& AbilitySpec : GetActivatableAbilities()){if (!AbilitySpec.DynamicAbilityTags.HasTagExact(InputTag)) continue;AbilitySpecInputPressed(AbilitySpec);		//通知输入按下了if (!AbilitySpec.IsActive()){TryActivateAbility(AbilitySpec.Handle);		}}
}void UCC_AbilitySystemComponent::AbilityInputTagReleased(const FGameplayTag& InputTag)
{if (!InputTag.IsValid()) return;for (FGameplayAbilitySpec& AbilitySpec : GetActivatableAbilities()){if (!AbilitySpec.DynamicAbilityTags.HasTagExact(InputTag)) continue;AbilitySpecInputReleased(AbilitySpec);		//通知输入释放了}
}

其中:

/** Direct Input state replication. These will be called if bReplicateInputDirectly is true on the ability and is generally not a good thing to use. (Instead, prefer to use Generic Replicated Events). */UFUNCTION(Server, reliable, WithValidation)void ServerSetInputPressed(FGameplayAbilitySpecHandle AbilityHandle);UFUNCTION(Server, reliable, WithValidation)void ServerSetInputReleased(FGameplayAbilitySpecHandle AbilityHandle);/** Called on local player always. Called on server only if bReplicateInputDirectly is set on the GameplayAbility. */virtual void AbilitySpecInputPressed(FGameplayAbilitySpec& Spec);/** Called on local player always. Called on server only if bReplicateInputDirectly is set on the GameplayAbility. */virtual void AbilitySpecInputReleased(FGameplayAbilitySpec& Spec);

这段代码是Unreal Engine中Gameplay Ability System (GAS)的输入处理相关函数,主要用于处理游戏能力的输入同步。

函数功能说明:

  1. ServerSetInputPressed‌ - 服务器RPC函数,当客户端按下能力输入时调用,将输入状态同步到服务器
  2. ServerSetInputReleased‌ - 服务器RPC函数,当客户端释放能力输入时调用
  3. AbilitySpecInputPressed‌ - 本地输入按下处理函数,在本地玩家始终调用,服务器端仅在bReplicateInputDirectly为true时调用
  4. AbilitySpecInputReleased‌ - 本地输入释放处理函数,调用规则同上

架构特点:

  • 使用Unreal的RPC系统实现客户端-服务器通信
  • 通过AbilityHandle精确标识特定的游戏能力
  • 注释明确指出直接输入复制不是推荐做法,建议使用通用复制事件
  • 采用虚拟函数设计,支持子类重写自定义行为

这些函数构成了GAS中输入事件处理的基础框架,确保多人游戏中能力输入的准确同步。

效果:

4.在蓝图测试技能里添加鼠标左键:

测试:

http://www.dtcms.com/a/581789.html

相关文章:

  • [特殊字符] 常用 Maven 命令
  • 简单的智能数据分析程序
  • 网页制作元素有哪些前端角度实现网站首页加载慢优化
  • C++中的智能指针std::shared_ptr是线程安全的吗?以及它的详细实现原理
  • 网站服务器安装教程视频教程电子商务网站规划
  • 【vsftpd报错】227 Entering Passive Mode,553 Could not create file.
  • 有多少网站可以推广业务那个公司做app
  • 正规的大连网站建设a963中华室内设计官网
  • 中承信安信创软件检测:CMA资质+国家标准双重保障的测试报告
  • #智能CI/CD流水线与AIOps 论坛@AiDD深圳站
  • 医疗AI模型与控制器自动化CI/CD流水线
  • NumPy -数组运算与操作
  • 中美最近军事新闻邯郸网站优化公司
  • windows本机vscode通过ssh免密登录远程linux服务器 git push/pull 免密
  • go语言网站开发教程门户网站是如何做引流的
  • SG-ECAT_S-TCP(EtherCAT 转 ModbusTCP 网关)
  • 分享一些在C++中使用异常处理的最佳实践
  • 物流网站怎么开网络最好的运营商
  • 学习随笔-async和await
  • 祁阳做网站河南工程建设验收公示网
  • PCIe协议分析仪-VIAVI设置抓取ASPM协商过程
  • ThreadLocal 相关知识点
  • OSG新版GLSL语法全解析
  • 智守边界:入侵报警系统的主动防御时代
  • 为什么网站建设起来搜素不到电子商务网站建设考题
  • 济南网站建设是什么合肥seo网络营销推广
  • 【Hot100|4-LeetCode 283. 移动零 】
  • 操作系统拿着文件名查找磁盘文件的全过程
  • 【Hot100 | 6 LeetCode 15. 三数之和】
  • 哪些网站用wordpress建设银行网站总是崩溃