Add component to actor on PostEditChange..()

Hey,

I’ve got a component that is attached to a Pawn. The component has an array of (bone-)names. I’ve overriden the PostEditChangeChainProperty() method to react when a new item is added to the array ( in the details panel) .When a new item is added I’d like to create and add a component to the component’s owner, like when clicking the “Add Component” button in blueprint.

I’ve tried the following

#if WITH_EDITOR 
void MyComponent::PostEditChangeChainProperty(struct FPropertyChangedChainEvent& PropertyChangedEvent) {

	if (PropertyChangedEvent.ChangeType == EPropertyChangeType::ArrayAdd) {
		UBoxComponent* box = ConstructObject<UBoxComponent>(UBoxComponent::StaticClass(),GetTransientPackage(),BoneNames.Last());
		GetOwner()->AddOwnedComponent(box);
	}

	Super::PostEditChangeChainProperty(PropertyChangedEvent);
}
#endif

It crashes at GetOwner() due to nullptr.
What’s the proper way of doing it?

Edit:
I’ve found the desired method as

FKismetEditorUtilities::AddComponentsToBlueprint(UBlueprint* Blueprint, const TArray<UActorComponent*>& Components, bool bHarvesting, USCS_Node* OptionalNewRootNode, bool bKeepMobility /*= false*/)

But how do I get the UBlueprint pointer to the component’s owner?

UBlueprint is blueprint asset class, you can get it from UClass of blueprint class from ClassGeneratedBy variable ofcorse if class is from blueprint or else it’s null pointer

I’m afraid I don’t quite understand. Do you mind explaining this further? Thanks!

Call GetClass() on owner of component, this will return you UClass of the class of blueprint:

And there oyu have varable ClassGeneratedBy, it should point to UBlueprint that generated the class (atleats i assume), ofcorse you need to cast it to UBlueprint

Assets are UObjects that contains asset data and can be saved in file and each asset type has it’s own class. In case of Blueprints it is UBlueprint so this argument requires to you input blueprint asset.

I see, but I can’t call GetClass() on owner since GetOwner() returns null when in editor, doesn’t it?