{"id":94,"date":"2022-01-14T08:35:30","date_gmt":"2022-01-14T08:35:30","guid":{"rendered":"https:\/\/www.thegames.dev\/?p=94"},"modified":"2023-03-22T11:33:26","modified_gmt":"2023-03-22T11:33:26","slug":"activating-abilities-when-granted-to-asc-passive-abilities","status":"publish","type":"post","link":"https:\/\/www.thegames.dev\/?p=94","title":{"rendered":"Activating abilities when Granted to ASC (Passive Abilities)"},"content":{"rendered":"\n<p>Here is a good way to activate abilities that have been given to the ASC, useful for passive abilities (Abilities that automatically run without triggers\/user activation).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>First we will need to add a custom bool to your custom Gameplay Ability class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\n\/** Ability is passive and auto activates *\/\nUPROPERTY(EditDefaultsOnly, Category = Activation)\nbool bPassiveAbility = false;\n<\/pre><\/div>\n\n\n<p>And we need to override one function from the GameplayAbility and our custom function to activate the ability if it is passive<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nvirtual void OnGiveAbility(const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilitySpec&amp; Spec) override;\n\n\/\/Called to try to active the passive ability\nvoid TryActivatePassiveAbility(const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilitySpec&amp; Spec) const;\n<\/pre><\/div>\n\n\n<p>Now in the cpp file, lets implement the override above<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nvoid UKaosGameplayAbility::OnGiveAbility(const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilitySpec&amp; Spec)\n{\n\tSuper::OnGiveAbility(ActorInfo, Spec);\n\n\tTryActivatePassiveAbility(ActorInfo, Spec);\n}\n<\/pre><\/div>\n\n\n<p>And now onto the actual activation logic<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nvoid UKaosGameplayAbility::TryActivatePassiveAbility(const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilitySpec&amp; Spec) const\n{\n\tconst bool bIsPredicting = (Spec.ActivationInfo.ActivationMode == EGameplayAbilityActivationMode::Predicting);\n\n\t\/\/ Try to activate if activation type is Passive. Passive abilities are auto activated when given.\n\tif (ActorInfo &amp;&amp; !Spec.IsActive() &amp;&amp; !bIsPredicting &amp;&amp; bPassiveAbility))\n\t{\n\t\tUAbilitySystemComponent* ASC = ActorInfo-&gt;AbilitySystemComponent.Get();\n\t\tconst AActor* AvatarActor = ActorInfo-&gt;AvatarActor.Get();\n\n\t\t\/\/ If avatar actor is torn off or about to die, don&#039;t try to activate it.\n\t\tif (ASC &amp;&amp; AvatarActor &amp;&amp; !AvatarActor-&gt;GetTearOff() &amp;&amp; (AvatarActor-&gt;GetLifeSpan() &lt;= 0.0f))\n\t\t{\n\t\t\tconst bool bIsLocalExecution = (NetExecutionPolicy == EGameplayAbilityNetExecutionPolicy::LocalPredicted) || (NetExecutionPolicy == EGameplayAbilityNetExecutionPolicy::LocalOnly);\n\t\t\tconst bool bIsServerExecution = (NetExecutionPolicy == EGameplayAbilityNetExecutionPolicy::ServerOnly) || (NetExecutionPolicy == EGameplayAbilityNetExecutionPolicy::ServerInitiated);\n\n\t\t\tconst bool bClientShouldActivate = ActorInfo-&gt;IsLocallyControlled() &amp;&amp; bIsLocalExecution;\n\t\t\tconst bool bServerShouldActivate = ActorInfo-&gt;IsNetAuthority() &amp;&amp; bIsServerExecution;\n\n\t\t\tif (bClientShouldActivate || bServerShouldActivate)\n\t\t\t{\n\t\t\t\tASC-&gt;TryActivateAbility(Spec.Handle);\n\t\t\t}\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<p>The above code checks the state of the ability, checks if bPassiveAbility is true, then it checks the avatar actor to make sure its sane to activate this ability. Then we do some checks to make sure we are the correct network client or authority to activate the ability. For example we are server for Server Only and Server Initiated, or we are locally controlled for Local Predicted and LocalOnly.<\/p>\n\n\n\n<p>One other thing i also do is override InitAbilityActorInfo in the AbilitySystemComponent<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nvoid UKaosCoreAbilitySystemComponent::InitAbilityActorInfo(AActor* InOwnerActor, AActor* InAvatarActor)\n{\n\tSuper::InitAbilityActorInfo(InOwnerActor, InAvatarActor);\n\n\tif (InAvatarActor != nullptr)\n\t{\n\t\tABILITYLIST_SCOPE_LOCK();\n\t\tfor (const FGameplayAbilitySpec&amp; AbilitySpec : ActivatableAbilities.Items)\n\t\t{\n\t\t\tconst UKaosCoreGameplayAbility* KaosAbilityCDO = CastChecked&lt;UKaosCoreGameplayAbility&gt;(AbilitySpec.Ability);\n\t\t\tif (KaosAbilityCDO&amp;&amp; KaosAbilityCDO-&gt;bPassiveAbility)\n\t\t\t{\n\t\t\t\tKaosAbilityCDO-&gt;TryActivatePassiveAbility(AbilityActorInfo.Get(), AbilitySpec);\n\t\t\t}\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<p>Hope this helps people. You can always find me on www.unrealslackers.org.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a good way to activate abilities that have been given to the ASC, useful for passive abilities (Abilities that automatically run without triggers\/user activation). First we will need to add a custom bool to your custom Gameplay Ability class. And we need to override one function from the GameplayAbility and our custom function <a href=\"https:\/\/www.thegames.dev\/?p=94\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  Activating abilities when Granted to ASC (Passive Abilities)<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-gameplay-ability-system"],"rise-blocks_total_comments":0,"rise-blocks_categories":[{"term_id":4,"name":"Gameplay Ability System","slug":"gameplay-ability-system","term_group":0,"term_taxonomy_id":4,"taxonomy":"category","description":"Unreal Engine's Gameplay Ability System (GAS)","parent":2,"count":16,"filter":"raw","cat_ID":4,"category_count":16,"category_description":"Unreal Engine's Gameplay Ability System (GAS)","cat_name":"Gameplay Ability System","category_nicename":"gameplay-ability-system","category_parent":2}],"rise-blocks_excerpt":"Here is a good way to activate abilities that have been given to the ASC, useful for passive abilities (Abilities that automatically run without triggers\/user activation). First we will need to add a custom bool to your custom Gameplay Ability class. And we need to override one function from the GameplayAbility and our custom function to activate the ability if..","_links":{"self":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":4,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}