How to make actor react to clicking in C++

Hi, I tried to make my actor react to clicking.
The actor has a sphere component (invisible during play) and I have the following code in my Actor’s constructor:

OnClicked.AddDynamic(this, &AMyActor::DoOnClicked)

I have mouse cursor and click events enabled in my player controller, however my DoOnClicked method is not called.
I tried the same with AddDynamic on my SphereComponent but still no callback.

What am I missing?

Simple stack:

Check APlayerController::InputKey() and look on your settings

APlayerController::bEnableMouseOverEvents(it seems your aren’t using this stuff)


APlayerController::CurrentClickTraceChannel, maybe you haven’t set correctly trace channels on your spherecomponent.

Thanks. It seems like indeed trace channels were not setup correctly (although I don’t really know how they should be setup). When I setup:
APlayerController::DefaultClickTraceChannel = ECollisionChannel::ECC_WorldDynamic;

And then set the SphereComponent (in editor) collision to block all I have a crash:

#0	0x00007fff84179380 in _platform_memmove$VARIANT$Nehalem ()
#1	0x0000000100862894 in UObject::ProcessEvent(UFunction*, void*) ()
#2	0x00000001012daaac in AActor::ProcessEvent(UFunction*, void*) ()
#3	0x00000001013393e8 in void TMulticastScriptDelegate<FWeakObjectPtr>::ProcessMulticastDelegate<UObject>(void*) const ()
#4	0x0000000101739600 in UPrimitiveComponent::DispatchOnClicked() ()

Any idea what can be causing that? If I set those channels differently nothing happens.

I recall something about crash when I was tried to bind delegate from the ctor, try bind your delegate from other place than constructor, for example from PostInitializeComponents()

That crash started happening after I changed tracing channels on my player controller and in the actor’s blueprint. Happens even if I don’t bind delegates at all. And also when I do bound them in PostInitializeComponents();

Any ideas what might be causing that? And btw - what are the “correct” trace channel settings. I haven’t found any reasonable explanation of them.

Read this paper:

You can even create your own trace channels only for your actor.