hi all, sorry if this is a duplicate post. I just posted a question and it disappeared. Nevertheless, I am new to UE C++ and learning it using
Unreal Engine 4.x Scripting with C++ Cookbook - Second Edition. After reading many postings and watched quite a few YouTube videos, I still cannot figure out how to connect UGameplayAbility and UE5.1 Enchanced Input. The book uses the GameplayAbilitiesSet then bind the keys. Can someone shed some lights for me? Please see the code below from the book. Thanks so much.
include “AbilitySystemComponent.h”
// …
// Called to bind functionality to input
void AWarrior::SetupPlayerInputComponent(UInputComponent* Input)
{
Super::SetupPlayerInputComponent(Input);// Connect the class's AbilitySystemComponent // to the actor's input component AbilitySystemComponent->BindToInputComponent(Input); // Go thru each BindInfo in the gameplayAbilitySet. // Give & try and activate each on the // AbilitySystemComponent. for (const FGameplayAbilityBindInfo& BindInfo : gameplayAbilitySet->Abilities) { FGameplayAbilitySpec spec( // Gets you an instance of the UClass BindInfo.GameplayAbilityClass-> GetDefaultObject<UGameplayAbility>(), 1, (int32)BindInfo.Command); // STORE THE ABILITY HANDLE FOR LATER INVOKATION // OF THE ABILITY FGameplayAbilitySpecHandle abilityHandle = AbilitySystemComponent->GiveAbility(spec); // The integer id that invokes the ability // (ith value in enum listing) int32 AbilityID = (int32)BindInfo.Command; // CONSTRUCT the inputBinds object, which will // allow us to wire-up an input event to the // InputPressed() / InputReleased() events of // the GameplayAbility. FGameplayAbilityInputBinds inputBinds( // These are supposed to be unique strings that // define what kicks off the ability for the actor // instance. // Using strings of the format // "ConfirmTargetting_Player0_AbilityClass" FS("ConfirmTargetting_%s_%s", *GetName(), *BindInfo.GameplayAbilityClass->GetName()), FS("CancelTargetting_%s_%s", *GetName(), *BindInfo.GameplayAbilityClass->GetName()), "EGameplayAbilityInputBinds", // The name of the // ENUM that has the abilities listing // (GameplayAbilitySet.h). AbilityID, AbilityID ); // MUST BIND EACH ABILITY TO THE INPUTCOMPONENT, // OTHERWISE THE ABILITY CANNOT "HEAR" INPUT EVENTS. // Enables triggering of InputPressed() / // InputReleased() events, which you can in-turn use // to call // TryActivateAbility() if you so choose. AbilitySystemComponent->BindAbilityActivationToInputComponent( Input, inputBinds ); // Test-kicks the ability to active state. // You can try invoking this manually via your // own hookups to keypresses in this Warrior class // TryActivateAbility() calls ActivateAbility() if // the ability is indeed invokable at this time // according to rules internal to the Ability's class // (such as cooldown is ready and cost is met) AbilitySystemComponent->TryActivateAbility( abilityHandle, 1); }
}