How can I pass a skill function to gameplayability in C + +

I am learning gameplay ability system recently,And a simple a method to read the player’s residual energy value is realized.

I found that the execution logic of some skills was exposed in the blueprint,How can I implement this skill code and pass it to gameplay ability variable in C++ , Should I override activateability() in gameplayAbility.h?
I’m sorry for my poor English. I used google translate.

Hi, not sure if you need just one variable or the entire logic to be in C++. Yes indeed you can extend the UGameplayAbility class with your own custom class that executes your ability. If necessary, yes you can override ActivateAbility() as well. Your class can have any other variables that you need that can be set in blueprint etc., just like in other situations. Remember to commit your ability, whether in code or blueprint (I don’t see a CommitAbility node in your picture!)

Thank you for your answer.
In fact, I just want to put the implementation of skills into my extended MygameplayAbility.cpp.
But I don’t know how to add my code after the Node[Event ActivateAbility] as in the blueprint.

Hmmmm not sure I follow what you mean. If you want to call your c++ code as a node from within the Ability blueprint (GA_ blueprint basically), then define your function(s) as BlueprintCallable in your child UGameAbility class and you should be able to call them from your GA blueprint.

Thank you. This problem has been solved. But I encountered a new problem. When I call gascomponent - > giveability() in beginplay() and pass in the custom ability class, it will crash when the game is running.[alt text][1]

It would have been helpful to see the crash error log, but I think your code also is a bit problematic. You don’t need to create a new object for the ability in order to ‘give it’ to the character. The component does that for you behind the scenes. You need to pass it a class reference (I think you’re trying to pass an instantiated object in that SpecHandle, which I haven’t seen it work like that).
Create an FGameplayAbilitySpecHandle and pass it to GiveAbility like so:

const FGameplayAbilitySpec AbilitySpec([class type of your Ability], 1[this is Level input and 1 is fine, [some integer value for input code (make them unique for each ability if you can)]);
				BQAbilitySystemComponent->GiveAbility(AbilitySpec);

I tried your suggestion. Maybe I didn’t correctly understand the meaning of [class type of your ability], but my code couldn’t be compiled. Attached is a screenshot. I hope you can point out my error

Hey @vigilw , according to your last comment, your FGameplayAbilitySpec was wrong.

Ability spec needs these values in this order (exists other forms, but this is valid): (AbilityClass, AbilityLevel, AbilityInput, SourceObject)

You already have a enumeration for your inputs? Its a easy way to handle how keys will be binding to these abilities

A example:

UENUM(BlueprintType)
    enum class EAbilityInputID : uint8
    {
    	Skill_1,
    	Skill_2,
    	Skill_3,
    	Confirm,
    	Cancel
    };

And ‘ability class’ really need a class, you could create a function receiving a TSubclassOf as parameter.

Using these examples you can rewrite your function like this:

void APlayerClass::GiveAbility(const TSubclassOf<class UPlayerGameAbility> AbilityToAdd)
{
    if (GetLocalRole() != ROLE_Authority || !IsValid(GetAbilitySystemComponent()) || !IsValid(AbilityToAdd))
	     return;

    const auto& _Spec = FGameplayAbilitySpec(*AbilityToAdd, 1, static_cast<int32>(EAbilityInputID::Skill_1, this);
    GetAbilitySystemComponent()->GiveAbility(_Spec);
}