What's the best way of creating reusable components that render something and have behavior?

Hello everyone. I’m building a small test project to dive deeper into UE and I’m trying to figure out the best way of organizing the different pieces together.

Let me start with a bit of context. The game will be an FPS where both the player and the enemies can have a flashlight. Since I’m trying to make everything as modular as possible, I thought the best way of structuring this would be having some sort of FlashlightComponent that could then be attached to the different actors (player and enemies). This way, those actors would be able to interact with their FlashlightComponent in a way similar to the following:

if (FlashlightComponent) 
{
    FlashlightComponent->TurnOn();
    FlashlightComponent->TurnOff();
    FlashlightComponent->Toggle();
    // etc...
}

And the FlashlightComponent would take care of the implementation details like setting up a SpotlightComponent, enabling/dissabling it, etc.

Since I wanted to be able to attach this FlashlightComponent to an Actor and edit its transform in the Actor viewport, I assumed the best base class for it would be the SceneComponent. However, after deriving from it, I noticed there’s no way of attaching a SpotlightComponent to a SceneComponent (I thought that was allowed or what’s the purpose of SceneComponents otherwise) so I’m at a dead end.

Any ideas of how could I achieve this level of modularity using any other alternatives that I might not have discovered yet?

PS: I’ve also read a bit about using ChildActorComponents, however, I’ve seen a lot of opinions discouraging their usage.

PS2: Every online tutorial I’ve come across about how to implement a flashlight in UE attaches the components directly to the player actor, so if I had enemy actors, I would have to duplicate this functionality across the different actors, wouldn’t I?

Hey @larsbs!

Rather than a scene component or a child actor component, would deriving from the regular actor component and adding to your actors as a component not work for what you are trying to do? (Would attach to any actor whether player, pawn, etc.)

I hope the above is the solution you are looking for!

Hey, thanks for taking the time to reply! I saw that class already and in fact was my first option but, somewhere in the docs, it states that it should only be used to manage logic and state, so I discarded it as I wanted my component to also take care of rendering the spotlight. Is there anything I overlooked maybe?

Perhaps you can use a spotlight component:

image

A highly specialised Actor Component that comes with a spotlight. You can now encapsulate all necessary logic there and attach that extended component to world entities.

I also thought about doing that (and I think that could be the only solution in the end) but, since this is mostly a project to help me find a way to structure things, I’m trying to find a solution that could potentially be extended in the future.

For example, I was planning to create a (regular this time) ActorComponent called BatteryComponent that would take care of managing the energy of the flashlight (and this actor component could in turn be added to other “batter powered” devices).

In the above scenario, I guess there wouldn’t be a problem since BatteryComponent would not need to be rendered, but what if it needed? In that case, since apparently no component has a viewport, there’s no way of tweaking the transform visually from the editor, right?

PS: I’m starting to realize that I’m trying to do more or less what in Unity you would do nesting game objects. But in Unreal, you would do it nesting actors right? Like, I should spawn the FlashlightActor when the PlayerCharacter actor enters the game world and attach one to another?

So basically, I think what I want to know is if there is a way of creating reusable component sub-trees?