I have an Actor (a spaceship) that spawns its turrets dynamically at runtime. It uses the following code to do so, and works:
UChildActorComponent *child = NewObject<UChildActorComponent>(AttachedToPawn);
if (child)
{
UObject *gCDO = itemClass->GeneratedClass->GetDefaultObject();
child->ChildActorClass = TSubclassOf<AActor>(gCDO->GetClass());
child->OnComponentCreated();
child->AttachTo(AttachComponent, tmd->MountName);
child->bCreatedByConstructionScript = true;
child->RegisterComponent();
ATurretActor *turret = Cast<ATurretActor>(child->ChildActor);
turret->SetTurretData(tvd, GameData);
turret->MountData = tmd;
Turrets.Add(turret);
}
The bit with GetDefaultObject() is a bit messy, but I couldn’t find any way to convert a TSubclassOf(UObject)
to TSubclassOf(AActor)
, which is the only thing ChildActorClass will accept.
Now - this code spawns the turrets and they attach to the right places and when the ship moves, they move with it. All working.
However, if I eject, grab the ship pawn and move it around manually (any amount) all of the dynamic turrets despawn. I have a UChildActorComponent that I added to the Ship manually in the ships blueprint, and it doesn’t despawn.
So there’s obviously something I am missing here. What is it?