Download

How do you create a component at runtime?

I have an actor and I want to create and attach a component to it at runtime in BeginPlay for example. The problem is that NewObject is templated and requires you to know the type of component you want to create at compile time. But this is a dynamic situation and the type of component can change. So I have a pointer to the class default object (CDO) of the component I want to create. I can’t use StaticClass() on it since it is a CDO instance, so I can only get a UClass* of it by using MyComponent->GetClass(). How can I use this to create an actor component at runtime, or is there another way to do it?

I actually worked out that I could use the base class as the template soon after I posted. Good to have it validated by someone else though. Thanks.

Is there a way to do the same using blueprints? “Construct object from class” seems not to work with actor components.