美文网首页
三、RPGAbilitySystemComponent.h/cp

三、RPGAbilitySystemComponent.h/cp

作者: 珏_Gray | 来源:发表于2019-06-11 09:58 被阅读0次

    GameplayAbilitySystem

    从这篇文章开始,我们将目光转向Gameplay Ability System。
    推荐最新的关于GAS的视频,你也能下载到ppt:
    unreal-fest-europe-2019/using-the-gameplay-ability-system
    借用其中Concept部分,可以从整体上认知GAS中各个组件的作用。




    AbilitySystemComponent

    我们回到AbilitySystemComponent上。从上面的说明我们已经可以猜测到AbilitySystemComponent是一个庞大的类,"in charge of managing everything GAS related inside the actor"。事实上,的确如此。AbilitySystemComponent.h有1586行代码。看看官方给出对于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”来与Ability、Effect、Attribute交互,随着我们深入GAS,会接触到。


    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实例**/
        void GetActiveAbilitiesWithTags(const FGameplayTagContainer& GameplayTagContainer, TArray<URPGGameplayAbility*>& ActiveAbilities);
    
        /** Returns the default level used for ability activations, derived from the character */
          /** 获得当前释放技能的默认等级,从人物角色上获得(这个游戏设定,应该是人物等级==技能等级) */
        int32 GetDefaultAbilityLevel() const;
    
        /** Version of function in AbilitySystemGlobals that returns correct type */
           /** 使用 AbilitySystemGlobals 类的方法,返回特定actor实例上的abilitysystemcomponent实例 */
        static URPGAbilitySystemComponent* GetAbilitySystemComponentFromActor(const AActor* Actor, bool LookForComponent = false);
    
    };
    

    拓展了三个方法,具体作用看注释。


    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是对gameplay ability的包装,还包含了ability运行时必要的信息 */
        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)
        {
              /** 获得人物等级 */
            return OwningCharacter->GetCharacterLevel();
        }
        return 1;
    }
    
    URPGAbilitySystemComponent* URPGAbilitySystemComponent::GetAbilitySystemComponentFromActor(const AActor* Actor, bool LookForComponent)
    {
          /** 使用UAbilitySystemGlobals的方法,对结果进行cast */
        return Cast<URPGAbilitySystemComponent>(UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(Actor, LookForComponent));
    }
    
    

    现在,我们回顾一下。发现三个新添加的get方法都是跟actor有关:

    • GetActiveAbilitiesWithTags: 获得actor(owner)所有与tag匹配的ability实例
    • GetDefaultAbilityLevel:获得character(owner)的等级
    • GetAbilitySystemComponentFromActor: 获得任意Actor实例挂载的AbilitySystemComponent实例

    相关文章

      网友评论

          本文标题:三、RPGAbilitySystemComponent.h/cp

          本文链接:https://www.haomeiwen.com/subject/hflffctx.html