在Unreal Action RPG Game(虚幻Gameplay Ability System的示例项目)中,在URPGAttributeSet::PostGameplayEffectExecute()
中修改了角色(玩家或AI)的健康属性。
在那里,AActor* SourceActor
是通过FGameplayAbilityActorInfo::AvatarActor
设置的。
SourceActor = Source->AbilityActorInfo->AvatarActor.Get()
此变量用于获取字符 (ARPGCharacterBase* SourceCharacter
,派生类AActor
的源),这是施加伤害所必需的。
现在-获取字符-而不是直接从SourceActor
向下转换,代码尝试通过其 controller 预先获取 character ;并且即使失败,它也会直接使用SourceActor
:
SourceActor = Source->AbilityActorInfo->AvatarActor.Get();
SourceController = Source->AbilityActorInfo->PlayerController.Get();
if (SourceController == nullptr && SourceActor != nullptr)
{
if (APawn* Pawn = Cast<APawn>(SourceActor))
{
SourceController = Pawn->GetController();
}
}
// Use the controller to find the source pawn
if (SourceController)
{
SourceCharacter = Cast<ARPGCharacterBase>(SourceController->GetPawn());
}
else
{
SourceCharacter = Cast<ARPGCharacterBase>(SourceActor);
}
为什么使用控制器绕行?