How do you override a primitive component's events on your actor?

I have an actor that has a box component inside of it. How can I override the box’s OnClicked event in the same way that I can override the actor’s “ReceiveActorOnClicked” event? At least I think that that is the right actor event, I’m new to the C++ side of UE4.

If I were to override the actor’s version, I would use this as a signature in the header file -

virtual void ReceiveActorOnClicked() override;

However, I have no idea how to do the same thing for the box component.

I’ve attached a screenshot to show which event specifically I’m looking to override.

You are right on the actor side, which using you custom actor you just overwrite. For the component you should do the same, create you own component that inherits from the one you use and then you can overwrite it.

In case you want to replace the actor (for example a Pawn or a Character) you have to tell the engine to not spawn the default version of the component so you can take care of specifying your own. Most of the components of the base engine classes are created using ‘CreateOptionalDefaultSubobject’ so that

This is done passing on the PCIP within your custom actors constructor a function chain telling the super class which components should not get created with its default types, this is achieved using the ‘DoNotCreateDefaultSubobject’ function. Then in the same constructor you create it using your own using ‘CreateDefaultSubobject’ to force that no child classes can prevent the creating or using ‘CreateOptionalDefaultSubobject’ so you could again change the components in other child classes.

An example can be found in the constructors of ‘ACharacter’, ‘APawn’ and ‘APaperCharacter’ which prevents the creation if the character mesh component that comes from ‘ACharacter’.

If you have more questions just post them.

Hope I could help!

Hi Moss, thank you for answering. Could you possibly explain the difference between the method that you just suggested and this code snippet in the FPS tutorial in the UE wiki? Specifically this line -

CollisionComp->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);

It looks like that method could be applied to a component click event as well, right?

In those you add a callback (a function that you provide) that should be called on the given event. This is used for example in the GameplayAbilities system:

PrimComponent->OnComponentBeginOverlap.AddDynamic(this, &UAbilityTask_WaitOverlap::OnOverlapCallback);
		PrimComponent->OnComponentHit.AddDynamic(this, &UAbilityTask_WaitOverlap::OnHitCallback);

Using a custom component will give you the ability to do more customized tricks while not changing the behavior on using those events. Using a custom components is far more complicated to setup also.

In my projects I normally use custom components in many cases, so when I need any customization I can do it in less time, but again, this should be considered advanced.

If you just need to respond to the different events (BeginOverlap, Hit, EndOverlap, …) use the callbacks from your own Actor class.

Thanks a lot, that was an extremely helpful clarification!