在 GameplayAbility 插件中定义了一个 IAbilitySystemInterface 接口,该接口只有一个普通的 C++ 纯虚函数。这种定义也是我们在代码里用着比较舒服的。缺点就是本身在蓝图中用不了。
1
2
3
4
5
6
7
classGAMEPLAYABILITIES_APIIAbilitySystemInterface{GENERATED_IINTERFACE_BODY()/** Returns the ability system component to use for this actor. It may live on another actor, such as a Pawn using the PlayerState's component */virtualUAbilitySystemComponent*GetAbilitySystemComponent()const=0;};
if(Actor==nullptr){returnnullptr;}constIAbilitySystemInterface*ASI=Cast<IAbilitySystemInterface>(Actor);if(ASI){returnASI->GetAbilitySystemComponent();}if(LookForComponent){/** This is slow and not desirable */ABILITY_LOG(Warning,TEXT("GetAbilitySystemComponentFromActor called on %s that is not IAbilitySystemInterface. This slow!"),*Actor->GetName());returnActor->FindComponentByClass<UAbilitySystemComponent>();}returnnullptr;