I have a class that derives from USceneComponent. In its constructor, I’ve been creating a sub-component (a USphereComponent) via CreateDefaultSubObject. Recently, I wanted to make this shape be either a USphereComponent or UCapsuleComponent.
I moved the creation code from the constructor to an overridden InitializeComponent, and changed it to use NewObject, RegisterComponent, and AttachToComponent. This works, but I can’t save the object anymore! I get an error that the “Graph is linked to external private object.”
What is the correct way to make a sub-component outside of the constructor? Not at runtime, as I still want to adjust parameters in the editor.
Huh, that is almost exactly what I had. And is now exactly. And when I recreated my blueprint with the component… it worked. Then I went back and re-added an intermediate step I’d done, and re-broke it.
If I use your code and add this function, it won’t let me save a level that contains the blueprint because of the aforementioned “Graph is linked…” error:
void UMySceneComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
// SC is a private UPROPERTY, initially created in InitializeComponent
if(SC)
{
SC->DestroyComponent();
SC = nullptr;
}
SC = NewObject<USphereComponent>(this, USphereComponent::StaticClass());
// set some aspects based on properties...
SC->SetupAttachment(this);
SC->RegisterComponent();
}
Tried your code and not getting any errors. The sphere shows up post property change. Tried saving in a level as a bp component, then added an actor with a child actor component set to the class creating the sphere. All saves.
Argh, no luck. If the “SC” variable is a UPROPERTY, i get that error. If it’s not a UPROPERTY, I don’t get an error (but I also don’t see the created component anymore). Creating it with NewObject in PostEditChangeProperty just seems to not work.
That is a great read, and sheds a lot of light on what I’m seeing. I am indeed making a blueprint-spawned component, and it does look like PostEditChangeProperty is marking the object for deletion. I moved the sub-component creation into OnComponentCreated, and the problem is resolved! Thanks!!