I’m building an obstacles handler in C++.
I want to have the obstacle component which is instantiated from a class that can be set in blueprint.
In my .h file I have this to parametrize the class:
First of all, is your obstacle handler component really a child of child actor component? That seems a bit wrong. I would guess it’s a child of UActorComponent. Either way, if you do have a parent C++ class for your obstacle handler, let’s call it UObstacleHandlingComponent, then your code would look something like this:
Then in your code, which would probably have to be on begin play, because you can’t use the value of a property in your constructor as far as I know, you would do something like:
// Instead of If you could also do a check, since I'm guessing this should never be null
if (ObstacleComponentClass)
{
ObstacleHandlingComponent = NewObject<UObstacleHandlingComponent>(this, ObstacleComponentClass);
ObstacleHandlingComponent->Activate();
ObstacleHandlingComponent->RegisterComponent();
}
else
{
// Throw some error
}
The UChildActorComponent is a UActorComponent subclass that lets you attach an AActor to another AActor. UStaticMeshComponent is a subclass of UActorComponent.
(And USceneComponent, and UPrimitiveComponent, and UMeshComponent in turn)
In general, “child” means scene attachment hierarchy, whereas “subclass” means class hierarchy. Those are different concepts. TSubclassOf<> works with the C++ class hierarchy.