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í)例