【UE4C++】获取Actor、Controller、Pawn、Character 获取 Actor
TActorIterator 遍历
可以⽤于遍历 Actor,也可以⽤于遍历 Component
for (TActorIterator<AStaticMeshActor> ActorIter(GetWorld()); ActorIter; ++ActorIter)
{
UKismetSystemLibrary::PrintString(GetWorld(), FString::Printf(TEXT("%s"), *ActorIter->GetName()));
}
UKismetSystemLibrary::GetAllActorsOfClass
Syntax
static void GetAllActorsOfClass(const UObject* WorldContextObject, TSubclassOf<AActor> ActorClass, TArray<AActor*>& OutActors);
代码实现
TArray<AActor*> OutActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AStaticMeshActor::StaticClass(), OutActors);
for (AActor* actor : OutActors)
{
UKismetSystemLibrary::PrintString(GetWorld(), actor->GetName());
}
UKismetSystemLibrary::GetAllActorsWithTag
Syntax
static void GetAllActorsWithTag(const UObject* WorldContextObject, FName Tag, TArray<AActor*>& OutActors); UKismetSystemLibrary::GetAllActorsWithInterface
Syntax
static void GetAllActorsWithInterface(const UObject* WorldContextObject, TSubclassOf<UInterface> Interface, TArray<AActor*>& OutActors);
获取 Controller
可配合 Cast 转换成对应的 Controller
UKismetSystemLibrary::GetPlayerController
Syntax
static class APlayerController* GetPlayerController(const UObject* WorldContextObject, int32 PlayerIndex);
代码实现
APlayerController* playerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); UWorld::GetFirstPlayerController
代码实现
APlayerController* playerController = GetWorld()->GetFirstPlayerController();
获取 Pawn
UKismetSystemLibrary::GetPlayerPawn
代码实现
APawn* myPawn = Cast<ADrone>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0)); GetPawn
代码实现
APawn* myPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
html实现用户注册登录代码获取 Character
UGameplayStatics::GetPlayerCharacter
代码实现
ACharacter* myPawn = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0); GetCharacter
代码实现
ACharacter* myPawn = GetWorld()->GetFirstPlayerController()->GetCharacter();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。