How can I add multiple hitboxes actors with default OnHit behavior?

I am trying to add multiple hitboxes (UBoxComponent, USphereComponent, etc.) to different types of actors and specify a default OnHit-Event for those hitboxes and have the whole thing modular so I’m able to add those two hitboxes with their default behavior to any kind of Actor-Blueprint (Character, Pawn, StaticMeshActor, etc.)
(and hopefully be able to change the size and location of those hitboxes in each blueprint).

My idea was to create a custom component, containing those two hitboxes and the default events.
According to my understanding, such a component could later on be added to any Class-Blueprint to add the hitboxes and their functionality to this Blueprint.
I therefore created the following class (HitboxComponent.h):

UCLASS(meta=(BlueprintSpawnableComponent))
class UHitboxComponent : public UActorComponent
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Hitbox)
	TSubobjectPtr<class UBoxComponent> FirstHitbox;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Hitbox)
	TSubobjectPtr<class UBoxComponent> SecondHitbox;
	
};

And HitboxComponent.cpp:

UHitboxComponent::UHitboxComponent(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	FirstHitbox = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("First Hitbox"));
	SecondHitbox = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Second Hitbox"));
}

I was able to compile my code and run it without any problems.
But as soon as I try to add this HitboxComponent my Visual Studio breaks (OutputDevice.cpp on line 214) with the following error:

C:\GitHub\UnrealEngine\Engine\Source\Runtime\Engine\Private\ActorComponent.cpp(120): Ensure condition failed: Owner == NULL || Owner == GetOuter()
Component Second Hitbox is owned by HitboxCharacter_C_0 by has an Outer of Hitbox1.
UE4Editor-Win64-Debug.exe has triggered a breakpoint.

The error appears four times but the editor continues to run and the component is added to the actor.
But it seems, that the addition of the component is not working correctly as my Blueprint then looks like this:

After this the blueprint can no longer be compiled (breaks again and prints the same error as before) nor can it be saved anymore with the following error:

Can't save D:/Unreal Projects/DialogSystem/Content/Blueprints/HitboxCharacter.uasset: Graph is linked to object(s) in external map.
External Object(s):
BoxComponent_1
BoxComponent_0
  
Try to find the chain of references to that object (may take some time)?

I am not sure if it is just not possible to create a component with other components in it or if I am doing something wrong.

I would be very glad if someone could help me solve the problem (or help me get a better idea on how to implement this functionality).

Thanks in advance!

Hey, well let me see if I can help, I’m new to UE4 too,
I guess the easyest way to make several hitboxes would be to do it in Blueprint,
instead of using UBoxComponent, USphereComponent, you could also just make your own simple mesh - StaticMesh component and make it not visible in game. This could help you making best fit to what you want to do , and you would place it in correct location and set up the HitEvent on the staticMesh component. I would suggest you try to make it work with blueprints first, and then code it if you need to.
You could also use AttachTo, to position your components to specific Socket.
Maybe try attaching your components to the Root component as well:

FirstHitbox = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("First Hitbox"));
FirstHitbox ->AttachTo(RootComponent);

(Whenever I was changing the size of default Capsule component, same thing happened to me,
for some reason blueprint didn’t compile and editor crashed, so thats why I used simple StaticMesh and set a invisible material to it, and it works great and at same time fits exeactly.

Thanks for your answer!
I didn’t even notice I got an answer so excuse my late response :slight_smile:

I’ve tried your solution and it seems to work.
I did kind of the same thing meanwhile but instead of using a StaticMesh I created an Actor containing the Hitboxes.

I could then add the actor as a ChildActorComponent to my other Actor and got the functionality to work.
Sadly both variations aren’t exactly modular and easily updateable.

But at least they work, so thanks anyway! :slight_smile: