UProperties and Dynamically created components

I’ve been trying to figure out how to add components to an object at runtime. After looking it up, I found using NewObject followed by AttachTo would suffice, however, the editor exposes the editable properties of that component only within the parent, in my case, a bare bones Actor called SoundControl.

In this example, I create an Audio Component and attach it to the root as such:

.h

	UPROPERTY(EditAnywhere)
	UAudioComponent* Audio;

.cpp

(Called from BeginPlay)
Audio = NewObject<UAudioComponent>(this);
Audio->AttachTo(RootComponent);
Audio->RegisterComponent();

This results in modifiable properties from within SoundControl:

But under AudioComponent_5, you can see the properties can’t be modified:

While this is workable, I’m left wondering why I can’t just access them from the attached component. What can I do here to expose those properties?

This is a limitation on dynamically created components. I did a bit of digging and found that it will only allow editing if the component’s internal name is equal to that of a component on the class CDO. This will fail for dynamically created components since they don’t exist on the CDO.

From what I can tell, it’s not an inherent limitation of the system, just of the way the engine performs the check. I just tried a ridiculously ugly hack to trick it into thinking it matched a component on the CDO, and everything worked fine. Honestly I’m not entirely sure why the engine is so strict in trying to limit which components can be edited, I think this is something that could be relaxed.

What was your hack? :slight_smile:

The object within the Blueprint Viewport exists just after the constructor has been called. Since you are creating the object dynamically during BeginPlay, the properties and object you are trying to modify don’t exist yet. BeginPlay hasn’t been called.

What you can do is create properties within the containing actor that are then assigned to the dynamically created object after it is spawned, but you can’t set values on an object that doesn’t exist.