【UE4 C++】 获取Actor、Controller、Pawn、Character

66 篇文章 7 订阅

获取 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();
    

获取 Character

UGameplayStatics::GetPlayerCharacter

  • 代码实现

    ACharacter* myPawn = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
    

GetCharacter

  • 代码实现

    ACharacter* myPawn = GetWorld()->GetFirstPlayerController()->GetCharacter();
以下是一个简单的UE4 C++生成Actor的例子,假设你已经创建了一个新的C++项目: 1. 创建一个新的C++类,继承自AActor。例如,我们创建一个名为MyActor的类: ```cpp UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; }; ``` 2. 在MyActor.cpp文件中实现构造函数和BeginPlay函数: ```cpp AMyActor::AMyActor() { // 设置Actor的根组件 RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent")); // 创建一个静态网格组件 UStaticMeshComponent* MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent")); MeshComponent->SetupAttachment(RootComponent); // 加载静态网格 constructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")); if (Mesh.Succeeded()) { MeshComponent->SetStaticMesh(Mesh.Object); } } void AMyActor::BeginPlay() { Super::BeginPlay(); // 在控制台输出Actor的位置 UE_LOG(LogTemp, Warning, TEXT("MyActor is at %s"), *GetActorLocation().ToString()); } ``` 3. 在MyActor.cpp文件中实现Tick函数,使Actor在每帧旋转: ```cpp void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); // 每帧旋转Actor FRotator Rotation = GetActorRotation(); Rotation.Yaw += DeltaTime * 100.f; SetActorRotation(Rotation); } ``` 4. 在MyActor.h文件中添加头文件和构造函数声明: ```cpp #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Engine/StaticMesh.h" #include "Components/StaticMeshComponent.h" #include "MyActor.generated.h" UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true")) UStaticMeshComponent* MeshComponent; }; ``` 5. 在你的场景中添加一个MyActor,例如在GameMode的BeginPlay函数中: ```cpp void AMyProjectGameModeBase::BeginPlay() { Super::BeginPlay(); // 创建一个新的MyActor AMyActor* MyActor = GetWorld()->SpawnActor<AMyActor>(AMyActor::StaticClass(), FVector(0.f, 0.f, 0.f), FRotator(0.f, 0.f, 0.f)); } ``` 6. 运行游戏,你应该可以看到一个旋转的立方体Actor。 希望这个例子能够帮助你理解如何使用UE4 C++生成Actor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值