GAS是Epic提供的一套技能解决方案,使用时需要在plugin启用Gameplay Abilities插件。
//创建AbilitySystemComponent组件 AbilitySystemComponent = CreateDefaultSubobject(TEXT("AbilitySystemComponent")); //重写该方法 UAbilitySystemComponent* AGasLearningCharacter::GetAbilitySystemComponent() const { return AbilitySystemComponent; }
/** *key 要与项目input的设置一致,才能刚在gas里绑定 */ UENUM(BlueprintType) enum class EGASAbilityInputID : uint8 { //0 None None UMETA(DisplayName = "None"), //1 Jump Jump UMETA(DisplayName = "Jump"), //2 Sprint Sprint UMETA(DisplalyName = "Sprint") }; void Character::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) //绑定输入与释放的技能(通过枚举的key和Input的key一一对应) AbilitySystemComponent->BindAbilityActivationToInputComponent( PlayerInputComponent, FGameplayAbilityInputBinds( FString(),/**ConfirmTargetCommand*/ FString(),/**CancelTargetCommand*/ FString(TEXT("EGASAbilityInputID")),/**EnumName,即上面定义的枚举*/ static_cast(EGASAbilityInputID::None),/**ConfirmTargetInputID*/ static_cast(EGASAbilityInputID::None)/**CancelTargetInputID*/ ) ); } /*关于Confirm和Cancel: 技能基本走的是这样的套路: 技能激活 --> 选择目标 --> 确认/取消 目标 --> 释放技能效果/中断技能 **/
void Character::PossessedBy(AController* NewController) { Super::PossessedBy(NewController); if(GetLocalRole() != ROLE_Authority || !IsValid(AbilitySystemComponent)) { return /**处理多人游戏*/; } //给与技能 AbilitySystemComponent->GiveAbility( FGameplayAbilitySpec( UGameplayAbility_CharacterJump::StaticClass(),//使用GAS自带的跳跃技能实现 1, static_cast(EGASAbilityInputID::Jump), this) ); //SprintAbility可以在蓝图实现 //UPROPERTY(BlueprintReadOnly, EditAnywhere) //TSubclassOf SprintAbility; if(SprintAbility) { AbilitySystemComponent->GiveAbility( FGameplayAbilitySpec( SprintAbility, 1, static_cast(EGASAbilityInputID::Sprint), this ) ); } /*关于蓝图实现技能: 创建GameplayAbility的子蓝图,实现EventActivateAbility **/ }