I struggle to understand best practices regarding composition and inheritance for Actor Components in Unreal Engine 5. Concretely, I want to build a base class, say Interactable
that handles basic functionality of interactable items in the level. E.g. it detects collision, creates a prompt saying "Press [E] to interact", and then triggers an event or function when this happens. Subclasses can then implement this concretely.
Now, when I implement Interactable
as an Actor, it's all easy. I can add a Box Collision to the scene root and listen to events triggered by it. However, this seems like a bad practice, because an Actor then always ever be either Interactable
or, say, Destructible
.
It occurs to me that Interactable
should rather be an Actor Component instead. However, an actor component can neither have nor be a collision box. They cannot have a collision box because components cannot have components. I feel it should be possible that they can be a collision box, but at least for Blueprints I don't see a way to achieve that. It seems the superclass should be something like UBoxComponent. However, this is not available from the "New Blueprint" dialog. (Why?) The lowest ancestor is PrimitiveComponent
, but that, too, cannot be selected as a blueprint parent class. (Why?)
The only ways out I see are:
- Require that any Actor who wants to use the
Interactable
component also define a collision box. Then, in the component code ofInteractable
I can look it up and register events with it. This puts needless burden on the user and seems very odd. - When the
Interactable
component is constructed, add a collision box to the parent actor programmatically. This seems equally odd to me, and again is not user friendly because it will not be visible in the UI. - Use C++ I guess? I'd be fine with that in principle, but it seems this task is so simple that it lends itself well to the Blueprint concept, and I'd be surprised if this really only works in C++.
Is there a better one?