Include subclasses

Let’s say that I create a UCustomComponent (that extends UActorComponent) and then I create 4 different subclasses that extend UCustomComponent. Now I want to use these components in an actor-derived class. Do I need to include each single class in the class header file?

Ideally, you would not include those in your header files but your .cpp file instead. If you reference each component in your .h, you could forward declare the class like so, just after your includes :

class UCustomComponentA;
class UCustomComponentB;

Also, if you don’t need to directly specify a type of custom component, but only it parent, you can just forward declare that class, but still assign any of your custom component to it. For example, you could forward declare class UCustomComponent that is parent to UCustomComponentA and UCustomComponentB, add a reference to such a component (UCustomComponent* mMyCustomComponentPointer;) and in the .cpp you can assign it a pointer to a UCustomComponentA component (for example).

Hope this helps!


If you call subclass directly then yes, if just UCustomComponent then no

That’s exactly what I was looking for.
So if I have a virtual function DoSomething() that implements different behaviours for each UCustomComponentA,B,C and I write this in the .cpp:

mMyCustomComponentPointer = ObjectInitializer.CreateDefaultSubobject<UCustomComponentA>(this, ...);

When I access that function from the pointer which version of the function will be called?

The UCustomComponentA version! Since that’s what your object is :slight_smile:

Ok, thank you very much