美文网首页
三、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

    GameplayAbilitySystem 从这篇文章开始,我们将目光转向Gameplay Ability Sys...

  • 师大随景

    [cp]三月,不负好春光。 (图片来源:某同学) ​​​[/cp]

  • 至cp三

    第一件事:我想遇到一个她,一个可爱温柔,并喜欢在周末的早晨穿我的白衬衫瞎跑的她,我用温柔的抚摸她的秀发,轻生的讲述...

  • 我才不要和你做朋友呢

    分享截图 三对CP: 独步凌霄CP:李进步和段霄(最甜) 梧桐树CP:李青桐和吴智勋(be) 河童CP:李青桐和陈...

  • 剑网三:官方CP党,你站哪一对?

    《剑网三》截止今日,共上线13个门派,而这些门派都有着自己的CP,有官方CP,也有玩家们自己凑成的CP。所谓的CP...

  • 【梁山CP】烟雨(三)

    【烟雨】(三) 文:空山 这天梁湾下了夜班,外面下着寥寥细雨,医院分配的宿舍就在两条街外,很近,梁湾都是走着上下...

  • “三天cp”有感

    一次无聊的时间,看到了微信公众号里的“三天cp”,就报名参与了,报名申请也没有写什么内容,就写了自己的基本信息和对...

  • 打井一杨神仙传奇之三

    [cp]打井 ——杨神仙传奇之三 作者:蛰虫...

  • 2019-08-03

    [cp]三观不在一个频道上的人 不必互相说服 人生其实就是 不断筛选同频率思想的过程。 ​​​[/cp]

  • 我那逝去的青春(周一cp)

    读书时,我很喜欢看韩综!别人追我结的cp,我却追跑男的cp! 当年真的觉得他们是绝配!如今cp粉无家可归! 三年后...

网友评论

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

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