Add components in editor to actor in a level

So I have been trying to solve this problem for the last few days. Basically, I want to make a new blueprint asset through c++ and save it into the content browser in editor. The blueprint should derive from a custom actor class and I want to add actor components to it before saving it.

The current solution I have is using FKismetEditorUtilities::CreateBlueprintFromActor which will take a pointer to an actor in a level and use that as a template to make a blueprint. That works fine and the asset gets created with the correct inheritance. But the only problem is that I can’t seem to add any components to the actor in the level (before trying to create the blueprint). I am trying to call AActor::AddComponentByClass but that does nothing. Upon setting breakpoints I can see that it exits the method because World->bIsTearingDown is true and I am not sure why. I am creating the actor just before trying to add the components, so maybe the actor is not yet initialized correctly?

This is the code in question (DataSpecs->ModuleWall is a TSubclassOf UStaticMeshComponent):

UWorld* World = GEditor->GetEditorWorldContext().World();
AFGLevelModule* LevelModuleInstance = World->SpawnActor<AFGLevelModule>(AFGLevelModule::StaticClass());
LevelModuleInstance->AddComponentByClass(DataSpecs->ModuleWall, false, FTransform(), false);

Any ideas?

I managed to solve it in the end by refering to a similar implementation in the source code in a system known as DatasmithImporter. Apparently AddComponentsByClass doesn’t work when you want to save those components to a blueprint afterwards. This is the final implementation that worked for me:

UWorld* CurrentWorld = GEditor->GetEditorWorldContext().World();
		
AFGLevelModule* LevelModuleInstance = World->SpawnActor<AFGLevelModule>(AFGLevelModule::StaticClass());

USceneComponent* Component = NewObject<USceneComponent>(LevelModuleInstance, USceneComponent::StaticClass(), FName(TEXT("Component")), RF_Transactional);
LevelModule->AddInstanceComponent(Component);
Component->AttachToComponent(LevelModule->GetRootComponent(), FAttachmentTransformRules(EAttachmentRule::KeepWorld, false));
Component->SetRelativeLocation(RelativePosition);

UBlueprint* Blueprint = FKismetEditorUtilities::CreateBlueprintFromActor(PackageName, LevelModuleInstance, Params);