I have a problem where the actors do not properly attach to the parent actor’s SceneRootComponent. Any help would be appreciated!
I have an cpp actor named Terrain_Main_V3_1. I then have a blueprint actor that inherits from the cpp actor. In the blueprint I run the following function. The Target Component that I send in is the SceneRootComponent. However, their must be something wrong as the newly spawned actors do not attach to the SceneRootComponent. I wish to have them attached similar to the image:
void ATerrain_Main_V3_1::SpawnHexagons(TArray<FVector> VertexPosGrouped, USceneComponent* TargetComponent)
{
// Ensure VertexPos is a multiple of 7
if (VertexPosGrouped.Num() % 7 == 0)
{
// Calculate number of hexagons to spawn
int32 NumHexagons = VertexPosGrouped.Num() / 7;
// Iterate through each group of 7 vertices
for (int32 HexIndex = 0; HexIndex < NumHexagons; ++HexIndex)
{
int32 StartIndex = HexIndex * 7;
// Extract vertices for current hexagon
TArray<FVector> HexagonVertices;
for (int32 VertIndex = 0; VertIndex < 7; ++VertIndex)
{
HexagonVertices.Add(VertexPosGrouped[StartIndex + VertIndex]);
}
//const TArray<FVector> HexagonVerticesConst = HexagonVertices;
//Spawn hexagon
AHexagonData* NewHexagon = GetWorld()->SpawnActor<AHexagonData>(AHexagonData::StaticClass(), GetActorTransform(),FActorSpawnParameters());
UE_LOG(LogTemp, Warning, TEXT("Spawning Actor"));
if (NewHexagon)
{
// Assuming NewHexagon is a subclass of AActor and you want to attach it to the actor that spawned it
NewHexagon->AttachToComponent(TargetComponent, FAttachmentTransformRules::SnapToTargetIncludingScale);
NewHexagon->Initialize(HexagonVertices);
UE_LOG(LogTemp, Warning, TEXT("Attaching Actor"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Error Encountered"));
}
}
}
}