如何添加Actor并在虚幻编辑器中移动它?

时间:2014-07-01 06:36:39

标签: unreal-engine4

我是游戏开发的新手。通过培训课程入门(https://docs.unrealengine.com/latest/INT/Programming/QuickStart/7/index.html)我创建了一个类AMyActorTest扩展AActor:

#include "TestUProject.h"
#include "MyActorTest.h"


AMyActorTest::AMyActorTest(const class FPostConstructInitializeProperties& PCIP)
    : Super(PCIP)
{
    MyNumber = 12;
}

void AMyActorTest::BeginPlay()
{
    Super::BeginPlay();

    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Hello World!"));
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(MyNumber));
    }

}

我有一个问题,在将它放入ViewPort后,我无法在编辑器中移动到AActor。我读到我为我的演员丢失了RootComponent,但我不明白如何添加它(也许我不完全理解演员)。可以帮助您使用我的源代码来解决我的问题吗?此代码正在进行培训。 我的目标 - 添加一个演员并能够移动和旋转它。

1 个答案:

答案 0 :(得分:1)

请添加此代码

RootComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("Root"));

到你的构造函数。就这样。如果您想添加其他组件,可以使用类似的代码(此示例创建 UInstancedStaticMeshComponent

UInstancedStaticMeshComponent* instancedComp = PCIP.CreateDefaultSubobject<UInstancedStaticMeshComponent>(RootComponent, TEXT("SubMeshInstanced"));
instancedComp->AttachTo(RootComponent);  // this is important!

// this part is specific to this component 
// (although all are common to other types of your Root subitems)

instancedComp->SetStaticMesh(mesh);       

instancedComp->SetMaterial(0, material);
instancedComp->bOwnerNoSee = false;
instancedComp->bCastDynamicShadow = false;
instancedComp->CastShadow = false;
instancedComp->SetHiddenInGame(false);
instancedComp->SetMobility(EComponentMobility::Static);