三、RPGAbilitySystemComponent.h/cpp & AbilitySystemComponent

GameplayAbilitySystem

從這篇文章開始,我們將目光轉(zhuǎn)向Gameplay Ability System。
推薦最新的關(guān)于GAS的視頻,你也能下載到ppt:
unreal-fest-europe-2019/using-the-gameplay-ability-system
借用其中Concept部分,可以從整體上認(rèn)知GAS中各個(gè)組件的作用。




AbilitySystemComponent

我們回到AbilitySystemComponent上。從上面的說明我們已經(jīng)可以猜測(cè)到AbilitySystemComponent是一個(gè)龐大的類,"in charge of managing everything GAS related inside the actor"。事實(shí)上,的確如此。AbilitySystemComponent.h有1586行代碼??纯垂俜浇o出對(duì)于AbilitySystemComponent的說明:

/** 
 *  UAbilitySystemComponent 
 *
 *  A component to easily interface with the 3 aspects of the AbilitySystem:
 *  
 *  GameplayAbilities:
 *      -Provides a way to give/assign abilities that can be used (by a player or AI for example)
 *      -Provides management of instanced abilities (something must hold onto them)
 *      -Provides replication functionality
 *          -Ability state must always be replicated on the UGameplayAbility itself, but UAbilitySystemComponent provides RPC replication
 *          for the actual activation of abilities
 *          
 *  GameplayEffects:
 *      -Provides an FActiveGameplayEffectsContainer for holding active GameplayEffects
 *      -Provides methods for applying GameplayEffects to a target or to self
 *      -Provides wrappers for querying information in FActiveGameplayEffectsContainers (duration, magnitude, etc)
 *      -Provides methods for clearing/remove GameplayEffects
 *      
 *  GameplayAttributes
 *      -Provides methods for allocating and initializing attribute sets
 *      -Provides methods for getting AttributeSets
 *  
 */

AbilitySystemComponent提供了豐富的“method”來(lái)與Ability、Effect、Attribute交互,隨著我們深入GAS,會(huì)接觸到。


URPGGameAbilitySystemComponent.h

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "ActionRPG.h"
#include "AbilitySystemComponent.h"
#include "Abilities/RPGAbilityTypes.h"
#include "RPGAbilitySystemComponent.generated.h"

class URPGGameplayAbility;

/**
 * Subclass of ability system component with game-specific data
 * Most games will need to make a game-specific subclass to provide utility functions
 */
UCLASS()
class ACTIONRPG_API URPGAbilitySystemComponent : public UAbilitySystemComponent
{
    GENERATED_BODY()

public:
    // Constructors and overrides
    URPGAbilitySystemComponent();

    /** Returns a list of currently active ability instances that match the tags */
       /**   返回匹配tag的所有激活的ability實(shí)例**/
    void GetActiveAbilitiesWithTags(const FGameplayTagContainer& GameplayTagContainer, TArray<URPGGameplayAbility*>& ActiveAbilities);

    /** Returns the default level used for ability activations, derived from the character */
      /** 獲得當(dāng)前釋放技能的默認(rèn)等級(jí),從人物角色上獲得(這個(gè)游戲設(shè)定,應(yīng)該是人物等級(jí)==技能等級(jí)) */
    int32 GetDefaultAbilityLevel() const;

    /** Version of function in AbilitySystemGlobals that returns correct type */
       /** 使用 AbilitySystemGlobals 類的方法,返回特定actor實(shí)例上的abilitysystemcomponent實(shí)例 */
    static URPGAbilitySystemComponent* GetAbilitySystemComponentFromActor(const AActor* Actor, bool LookForComponent = false);

};

拓展了三個(gè)方法,具體作用看注釋。


URPGGameAbilitySystemComponent.cpp

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "Abilities/RPGAbilitySystemComponent.h"
#include "RPGCharacterBase.h"
#include "Abilities/RPGGameplayAbility.h"
#include "AbilitySystemGlobals.h"

URPGAbilitySystemComponent::URPGAbilitySystemComponent() {}

void URPGAbilitySystemComponent::GetActiveAbilitiesWithTags(const FGameplayTagContainer& GameplayTagContainer, TArray<URPGGameplayAbility*>& ActiveAbilities)
{
      /** FGameplayAbilitySpec是對(duì)gameplay ability的包裝,還包含了ability運(yùn)行時(shí)必要的信息 */
    TArray<FGameplayAbilitySpec*> AbilitiesToActivate;

          /** UAbilitySystemComponent的方法,獲得所有可激活的ability specification*/
    GetActivatableGameplayAbilitySpecsByAllMatchingTags(GameplayTagContainer, AbilitiesToActivate, false);

    // Iterate the list of all ability specs
    for (FGameplayAbilitySpec* Spec : AbilitiesToActivate)
    {
        // Iterate all instances on this ability spec
        TArray<UGameplayAbility*> AbilityInstances = Spec->GetAbilityInstances();

        for (UGameplayAbility* ActiveAbility : AbilityInstances)
        {
            ActiveAbilities.Add(Cast<URPGGameplayAbility>(ActiveAbility));
        }
    }
}

int32 URPGAbilitySystemComponent::GetDefaultAbilityLevel() const
{
    ARPGCharacterBase* OwningCharacter = Cast<ARPGCharacterBase>(OwnerActor);

    if (OwningCharacter)
    {
          /** 獲得人物等級(jí) */
        return OwningCharacter->GetCharacterLevel();
    }
    return 1;
}

URPGAbilitySystemComponent* URPGAbilitySystemComponent::GetAbilitySystemComponentFromActor(const AActor* Actor, bool LookForComponent)
{
      /** 使用UAbilitySystemGlobals的方法,對(duì)結(jié)果進(jìn)行cast */
    return Cast<URPGAbilitySystemComponent>(UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(Actor, LookForComponent));
}

現(xiàn)在,我們回顧一下。發(fā)現(xiàn)三個(gè)新添加的get方法都是跟actor有關(guān):

  • GetActiveAbilitiesWithTags: 獲得actor(owner)所有與tag匹配的ability實(shí)例
  • GetDefaultAbilityLevel:獲得character(owner)的等級(jí)
  • GetAbilitySystemComponentFromActor: 獲得任意Actor實(shí)例掛載的AbilitySystemComponent實(shí)例
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近無(wú)意間接觸到兒童立體書,這個(gè)產(chǎn)品確實(shí)不錯(cuò),如何生產(chǎn)出消費(fèi)者滿意甚至有點(diǎn)意外的產(chǎn)品,是企業(yè)不懈努力的目標(biāo)。關(guān)于這...
    心如止水8888閱讀 355評(píng)論 0 0
  • 生活總是不斷的失去,卻又不斷的獲得。手機(jī)就像現(xiàn)代人的回憶錄,每次打開,所有不經(jīng)意痕跡都告訴你誰(shuí)曾經(jīng)來(lái)過,誰(shuí)有...
    小天使9817閱讀 271評(píng)論 0 2
  • 緣分,fate,destiny ,我總覺得無(wú)論中文還是英文這個(gè)詞總是充滿浪漫主義色彩。日常生活中我應(yīng)該是個(gè)佛系青年...
    你看我這樣可愛不可愛閱讀 146評(píng)論 0 0
  • 他很多時(shí)候不知道怎么來(lái)思考,因?yàn)樗偸且葎e人慢一步,前半學(xué)期可以跟的上老師的步伐后面就越落越遠(yuǎn)直到上學(xué)被老師經(jīng)常...
    18_7fef閱讀 282評(píng)論 0 1

友情鏈接更多精彩內(nèi)容