StaticMesh onclicked

I’m having trouble figuring out how to handle click events for an Actor mesh in c++ code.

Everything is set up in the editor, I can add a onclick event to the mesh in the blueprint and it works fine with my planned logic, but I want to have this in the code.

I looked around and found this: OnClicked Parameters - Programming & Scripting - Unreal Engine Forums
However I am not having any luck with my method being triggered.

cpp file

 AShipActor::AShipActor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){
ShipMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ShipMesh"));
	ShipMesh->OnClicked.AddDynamic(this, &AShipActor::OnClicked);

void AShipActor::OnClicked(UPrimitiveComponent* pComponent){
	//my logic

Any help would be greatly appreciated!

Hey, have you enabled click events in the controller?

I’m not sure but i think you can’t set collision based events on mesh which may be quite complex shape, it would be preference issue, try using some shape component on top of a mesh as click hitbox

I changed my implementation to follow the TowerDefence example project, in which click events are handled in the PlayerController, and the click determines if an actor is underneath the cursor, which you can call methods on and perform onclick logic.

Can you please provide me the link for TowerDefence example project? I have same problem, could you more explanation is possible.