How to do an OnClicked event on an actor in C++

OnClicked is a delegate event

Delagate is equivalent of event dispatchers in blueprints, they exist so you can detect events from outside of object that event happanes because normal function call events can have code only in object they event triggers. In C++ it is variable in which you bind functions and when even happens (Broadcast or Execute is being called on delegate) it executes all binded functions to the delegate.

Since we dealing here with dynamic multi cast to bind a function you need to do this:

DelegateVarable.AddDynamic(ObjectToBeCalled, &ClassWhereFunctionIsDeclered::NameOfFunction);

So for example (assuming we binding in same actor where target function is):

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

You can do binding in BeginPlay for example.

2 Likes