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

**URL:** <https://forums.unrealengine.com/t/how-to-do-an-onclicked-event-on-an-actor-in-c/488523>\
**Category:** C++\
**Tags:** click-event, unreal-engine, CPP, question\
**Created:** [September 7, 2021, 9:14pm UTC](https://forums.unrealengine.com/t/how-to-do-an-onclicked-event-on-an-actor-in-c/488523 "2021-09-07T21:14:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DigitalCyan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DigitalCyan](https://forums.unrealengine.com/u/DigitalCyan)\
**Post date:** [September 7, 2021, 9:14pm UTC](https://forums.unrealengine.com/t/how-to-do-an-onclicked-event-on-an-actor-in-c/488523/1 "2021-09-07T21:14:19Z")

</div>

How would one raise an event on a clicked actor? I had an idea of raycasting to the character from a camera and checking if its an instance of Interactive then activate its public func but that seems botchy now when I know that in there somewhere there’s a built in function for a such thing. I was looking around but I couldn’t find a plain implementation of it or the one that compiled. Perhaps I was missing a header declaration or something. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [September 8, 2021, 11:33am UTC](https://forums.unrealengine.com/t/how-to-do-an-onclicked-event-on-an-actor-in-c/488523/2 "2021-09-08T11:33:59Z")

</div>

OnClicked is a delegate event

> **[OnClicked](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/AActor/OnClicked/)**
>
> Called when the left mouse button is clicked while the mouse is over this actor and click events are enabled in the player controller.

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.

---

<div class="post-metadata">

**Author:** ![DanathMorrin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/danathmorrin/32/592574_2.png) [@DanathMorrin](https://forums.unrealengine.com/u/DanathMorrin)\
**Post date:** [August 19, 2022, 1:32pm UTC](https://forums.unrealengine.com/t/how-to-do-an-onclicked-event-on-an-actor-in-c/488523/3 "2022-08-19T13:32:23Z")

</div>

A good explanation of this topic with code examples is here:

> **[\[UE4\]3 ways of adding OnClicked Event for Actor(both CPP and Blueprint)](https://dawnarc.com/2018/01/ue43-ways-of-adding-onclicked-event-for-actorboth-cpp-and-blueprint/)**
>
> keywords：如何获取鼠标点击时的物体对象、鼠标悬停事件、鼠标点击事件、鼠标单击事件
> Mouse Clicked Event Method 1: GetHitResult 1，Project Settings中编辑Input配置 如果要用鼠标模式触屏输入方式，如要勾选Use Mouse for Touch。
> 2，C++中注册回调：先给PlayerController注册Input事件
> void...
