Dynamically created component not visible/editable in components window

This is the code I use to add a dynamic component. Thanks to Tarquiscani0 for leading me to a full answer.

UMyComponent* MyNewComponent= NewObject<UMyComponent>(OwningActor);
MyNewComponent->SetupAttachment(ComponentToAttachTo);
OwningActor->AddInstanceComponent(MyNewComponent);
Owner->AddOwnedComponent(MyNewComponent);
MyNewComponent->RegisterComponent();

AddInstanceComponent() will set the CreationMethod to EComponentCreationMethod::Instance for you :slight_smile:

As previously noted, this will not update the SCSEditor embedded in the details panel.
To avoid having to manually click out and back in, use the following Editor only code to trigger a refresh in PostEditChangeProperty() on your component.

#include "MyComponent.h"

#if WITH_EDITOR
#include "UnrealEdGlobals.h"
#include "Editor/UnrealEdEngine.h"
#endif

...

#if WITH_EDITOR
void UMyComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent){
     //call Super first! Updates parent-child relationships
    Super::PostEditChangeProperty(PropertyChangedEvent); 
    GUnrealEd->UpdateFloatingPropertyWindows();
}
#endif

Since this introduces a dependency on GEditor you should add the following snippet in your .build.cs for the module to include UnrealEd in editor builds.

if (Target.bBuildEditor == true)
{
	PublicDependencyModuleNames.AddRange(new string[]
	{
		"UnrealEd"
	});
}
5 Likes