How to give an ability using C++?

Hello,

I spent lot of time trying to do this on my own, but can’t get my head around it.
I went through tutorial to enable GameplayAbility system and stopped on a fragment, where I create parameter for UGameplayAbility:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Abilities)
TSubclassOf<class UGameplayAbility> Ability;

Then tutorial sent me to assign ability:

void AARPGCharacter::BeginPlay()
{
	Super::BeginPlay();
	if (AbilitySystem)
	{
		if (HasAuthority() && Ability)
		{
			AbilitySystem->GiveAbility(FGameplayAbilitySpec(Ability.GetDefaultObject(), 1, 0));
		}
		AbilitySystem->InitAbilityActorInfo(this, this);
	}
}

Now I can assign ability through parameter “Ability” in Blueprint and it’s working well. But I want to assign this value dynamically through code. How can I achieve that? I’m looking for something like (example just to understand my question):

AbilitySystem->GiveAbility("SomeAbilityByBlueprintName", 1, 0);
1 Like

Hi,I just solved this problem recently,But I’m not sure it’s standard practice.
If there is a better way, please share it with me.My English is not good. I use Google translation

void APlayerCharacter::BeginPlay()
{
	Super::BeginPlay();
	
	if (AbilitySystemComponent) {
		if (HasAuthority()) {
		//UplayerGameplayAbility is my Is my extension UGameplayAbility class
	    const FGameplayAbilitySpec AbilitySpec(UPlayerGameplayAbility::StaticClass(),1);
        AbilitySystemComponent->GiveAbility(AbilitySpec);
		}
	}
	//AbilitySystemComponent->InitAbilityActorInfo(this, this);

	
	
	
}