虚幻引擎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)的输入处理相关函数,主要用于处理游戏能力的输入同步。
函数功能说明:
- ServerSetInputPressed - 服务器RPC函数,当客户端按下能力输入时调用,将输入状态同步到服务器
- ServerSetInputReleased - 服务器RPC函数,当客户端释放能力输入时调用
- AbilitySpecInputPressed - 本地输入按下处理函数,在本地玩家始终调用,服务器端仅在bReplicateInputDirectly为true时调用
- AbilitySpecInputReleased - 本地输入释放处理函数,调用规则同上
架构特点:
- 使用Unreal的RPC系统实现客户端-服务器通信
- 通过AbilityHandle精确标识特定的游戏能力
- 注释明确指出直接输入复制不是推荐做法,建议使用通用复制事件
- 采用虚拟函数设计,支持子类重写自定义行为
这些函数构成了GAS中输入事件处理的基础框架,确保多人游戏中能力输入的准确同步。
效果:
4.在蓝图测试技能里添加鼠标左键:

测试:




