I have a base class for a component called UBrushDynamicComponent, and various component classes that derive from it.
On my actor class, I want to be able to have an array I can iterate over which contains all of the components on the actor that derive from that class.
In my header file I have the following array set up:
This should compile just fine, doesn’t it?
You probably had something like:
TArray<UActorComponent*> Comp;
Now, I am not quite sure why you are getting zero components: I am not actually testing the code now, but I have used it before. Would you try adding UBrushDynamicComponent (and not a subclass) to check if that’s the issue?
Also make sure you are calling the function in the correct actor, AFTER the initialization of the components (say, add the component in the Constructor and call the function in begin play: just for testing purposes, if this is not what you want )
Ah! OK, I’m very new to C++ so I think I’ve just stumbled upon a fundamental mistake in my understanding. I didn’t really understand that a subclass still is the parent class as well as being the subclass.
So in order to have an array that holds all the subclass components, I can simply do TArray<UBrushDynamicComponent*>. I don’t even need to use TSubclassOf. So I can simply do: