Hi there!
I’m making a pretty basic hex map; parent map actor with child cells.
The map size is controllable in-editor, with PostEditChangeProperty prompting the map to regenerate if the appropriate parameters are changed.
When I generate the cells and parent them to the Map object, it seems to work fine, however saving and reloading the level (or editor) has all the cells detached again.
I’ve tried AttachToActor, K2_AttachToActor, AttachRootComponentToActor, AttachToComponent in all kinds of variations with no success.
I tried using the EditorActorSubsystem without any success.
I’ve tried GEditor->ParentActor.
Am I missing some editor-time attach function in favour of a run-time one that isn’t intended to keep persistence?
Thanks!
Relevant code:
for (int x = -n; x <= n; x++) {
const int z1 = FMath::Max(-n, -x - n);
const int z2 = FMath::Min(n, -x + n);
for (int z = z1; z <= z2; z++) {
if (Hexes.Contains(FIntPoint(x, z)))
continue;
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
AHexCell* Cell = World->SpawnActor<AHexCell>(AHexCell::StaticClass(), FVector(0,0,0), FRotator(0,0,0), SpawnParams);
Cell->Init(x,z,Size);
Cell->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform);
Hexes.Add(FIntPoint(x, z), Cell);
}
}
Edit to add:
SOLVED!
For posterity:
The issue wasn’t with my parenting at all, but with the RootComponent of my map not being loaded in. The reason was that I had this line within my Constructor:
if(UWorld* World = GetWorld(); !World ||HasAnyFlags(RF_Transient)) return;
Which prevented it from recreating the RootComponent on load, as the world was not yet available.