This seems like it should be simple to find the answer for, but here it goes:
My goal: I want to be able to drag my TileGrid class (extends AActor) onto into the viewport, and for it to spawn some Tile objects. If I move the TileGrid object, I want the others to follow (with an offset location), but each object can also be moved individually when selected. Then when I delete the TileGrid object, I want all of the spawned Tile objects to be destroyed as well.
My solution to spawning the objects:
FActorSpawnParameters fp;
fp.Owner = this;
AActor* ATile = this->()->SpawnActor<AActor>(TileClass);
ATile->AttachRootComponentToActor(this, NAME_None, EAttachLocation::KeepRelativeOffset);
This works as expected: I have it inside the constructor (with the bAllowDuringConstructionScript flag set on fp) and I have it inside the PostEditChangeProperty for when the TileBaseClass gets changed in the editor.
The problem is when I come to delete the object in the editor. The children are detached and left hanging around afterwards and I’m at a loss as to how to make them also disappear.
- I tried storing an array and going through it in the OnDestroy function, but the array was empty when I got there.
- I tried accessing the Children array, but that was also empty in the OnDestroy function.
- I tried using the GetAttachedActors() array, also empty at OnDestroy.
I’ve just gotten wind that the solution, if I were using blueprints, might involve binding the Tile objects to be notified when the TileGrid object gets deleted. But I’m not 100% how I’d do this in blueprints, let alone using the UE4 code.