Hello! In this code I’m trying to fill a chunk with tiles. When I drag’n’drop an instance of my class into the scene, I see that only first tile moves correctly with the pivot.
AChunk::AChunk()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
UStaticMesh* tileMesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Game/World/Terrain/Plane1x1.Plane1x1'")).Object;
for (uint32 i = 0; i < 16; ++i)
{
for (uint32 j = 0; j < 16; ++j)
{
Tiles[i][j] = CreateDefaultSubobject<UStaticMeshComponent>(FName(*(FString("Tile") + FString::FromInt(i) + "." + FString::FromInt(j))));
Tiles[i][j]->RegisterComponent();
Tiles[i][j]->AttachTo(RootComponent);
Tiles[i][j]->SetStaticMesh(tileMesh);
Tiles[i][j]->SetRelativeLocation(FVector(50 + i * 100, 50 + j * 100, 0));
}
}
}
Hi TheShadowBox,
I seem to recall seeing something similar in some of the previous engine versions. The solution I’ve always used is to put that code in the BeginPlay function instead of the constructor. Have you tried this?
Hi , thank you for answering!
Yes, I tried. In BeginPlay() it works great, but my logic needs to be executed on construction.
I have found out the way to fix it. All I needed is to create a new scene component and use it as root instead of using first tile.
AChunk::AChunk()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
UStaticMesh* tileMesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Game/World/Terrain/Plane1x1.Plane1x1'")).Object;
Root = CreateDefaultSubobject<USceneComponent>(FName("Root"));
Root->AttachTo(RootComponent);
for (uint32 i = 0; i < 16; ++i)
{
for (uint32 j = 0; j < 16; ++j)
{
Tiles[i][j] = CreateDefaultSubobject<UStaticMeshComponent>(FName(*(FString("Tile") + FString::FromInt(i) + "." + FString::FromInt(j))));
Tiles[i][j]->AttachTo(Root);
Tiles[i][j]->SetStaticMesh(tileMesh);
Tiles[i][j]->SetRelativeLocation(FVector(50 + i * 100, 50 + j * 100, 0));
}
}
}