# C++ Event dispatchers: How do we implement them?

**URL:** <https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 7, 2015, 2:01am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185 "2015-03-07T02:01:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slayemin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Slayemin](https://forums.unrealengine.com/u/Slayemin)\
**Post date:** [March 7, 2015, 2:01am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/1 "2015-03-07T02:01:08Z")

</div>

Can we create event dispatchers in C++ and then expose them to blueprints which inherit from the C++ class?

If so, what would be the appropriate reflection macro to use for it?

---

<div class="post-metadata">

**Author:** ![vibgyorc6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vibgyorc6](https://forums.unrealengine.com/u/vibgyorc6)\
**Post date:** [March 7, 2015, 5:09am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/2 "2015-03-07T05:09:31Z")

</div>

Yes you can, with delegates.

> **[Delegates](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Delegates/)**
>
> Data types that reference and execute member functions on C++ Objects

---

<div class="post-metadata">

**Author:** ![eXi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/exi/32/906583_2.png) [@eXi](https://forums.unrealengine.com/u/eXi)\
**Post date:** [March 7, 2015, 11:03am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/3 "2015-03-07T11:03:53Z")

</div>

Hey there,

it happens that i did this 2 days ago. Since i want code examples in the docs, i will give you a small code example on how to setup a delegate  
to be used in Blueprints and C++.

First you need to add your Delegate through the macro in **your classes header** file:

```auto

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);

```

“FTestDelegate” represents the Typename of your upcomming delegates.  
You can use Params by adding \_OneParam or \_TwoParams and so on at the end.

```auto

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTestDelegate, PARAMTYPE, PARAMNAME);

Example:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTestDelegate, float, Damage);

```

You place this outside of your class declaration(example):

```auto

#pragma once

#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"

/**
 * 
 */

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);

UCLASS()
class SOCKETTEST_API AMyPlayerController : public APlayerController
{
	GENERATED_UCLASS_BODY()
	
	
public:
	virtual void Tick(float DeltaTime) override;
	virtual void BeginPlay() override;

        void TestFunction();

	UPROPERTY(BlueprintAssignable, Category = "Test")
		FTestDelegate OnTestDelegate;
};

```

Now you should have spotted the UPROPERTY at the bottom of my example.  
This is the part you add a Delegate (here called “OnTestDelegate”).  
The UPROPERTY “BlueprintAssignable” give you the possibility to bind/assign  
this function to a custom event in your BP Child class.

You should be able to bind a delegate in C++ AND Blueprint and call both  
bound functions/events through c++.

```auto

UPROPERTY(BlueprintAssignable, Category = "Test")
	FTestDelegate OnTestDelegate;

```

If you want to add a function to it, you need to call “AddDynamic” on the delegate.  
The editor won’t show you this and maybe shows an error. Just try to compile anyway…

Here is an example for the normal, non param delegate from above, **inside your CPP File** :

```auto

OnTestDelegate.AddDynamic(this, &AMyPlayerController::TestFunction);

```

If you now want to call your delegate inside your code do this at the desired position:

```auto

OnTestDelegate.Broadcast();

```

I hope this helps (:

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9cf15cf7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9cf15cf7](https://forums.unrealengine.com/u/anonymous_user_9cf15cf7)\
**Post date:** [July 3, 2015, 8:28am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/5 "2015-07-03T08:28:20Z")

</div>

Hello,

New to UE4 c++ API.

How do I subscribe to OnTestDelegate from a different c++ actor.

Thank you,  
.

---

<div class="post-metadata">

**Author:** ![maikklein](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maikklein/32/918666_2.png) [@maikklein](https://forums.unrealengine.com/u/maikklein)\
**Post date:** [July 3, 2015, 10:44am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/6 "2015-07-03T10:44:33Z")

</div>

> [@eXi;240068](#):
>
> Hey there,
> 
> it happens that i did this 2 days ago. Since i want code examples in the docs, i will give you a small code example on how to setup a delegate  
> to be used in Blueprints and C++.
> 
> First you need to add your Delegate through the macro in **your classes header** file:
> 
> ```auto
> 
> DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);
> 
> ```
> 
> “FTestDelegate” represents the Typename of your upcomming delegates.  
> You can use Params by adding \_OneParam or \_TwoParams and so on at the end.
> 
> ```auto
> 
> DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTestDelegate, PARAMTYPE, PARAMNAME);
> 
> Example:
> 
> DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTestDelegate, float, Damage);
> 
> ```
> 
> You place this outside of your class declaration(example):
> 
> ```auto
> 
> #pragma once
> 
> #include "GameFramework/PlayerController.h"
> #include "MyPlayerController.generated.h"
> 
> /**
> * 
> */
> 
> DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);
> 
> UCLASS()
> class SOCKETTEST_API AMyPlayerController : public APlayerController
> {
> GENERATED_UCLASS_BODY()
> 	
> 	
> public:
> virtual void Tick(float DeltaTime) override;
> virtual void BeginPlay() override;
> 
> void TestFunction();
> 
> UPROPERTY(BlueprintAssignable, Category = "Test")
> FTestDelegate OnTestDelegate;
> };
> 
> ```
> 
> Now you should have spotted the UPROPERTY at the bottom of my example.  
> This is the part you add a Delegate (here called “OnTestDelegate”).  
> The UPROPERTY “BlueprintAssignable” give you the possibility to bind/assign  
> this function to a custom event in your BP Child class.
> 
> You should be able to bind a delegate in C++ AND Blueprint and call both  
> bound functions/events through c++.
> 
> ```auto
> 
> UPROPERTY(BlueprintAssignable, Category = "Test")
> FTestDelegate OnTestDelegate;
> 
> ```
> 
> If you want to add a function to it, you need to call “AddDynamic” on the delegate.  
> The editor won’t show you this and maybe shows an error. Just try to compile anyway…
> 
> Here is an example for the normal, non param delegate from above, **inside your CPP File** :
> 
> ```auto
> 
> OnTestDelegate.AddDynamic(this, &AMyPlayerController::TestFunction);
> 
> ```
> 
> If you now want to call your delegate inside your code do this at the desired position:
> 
> ```auto
> 
> OnTestDelegate.Broadcast();
> 
> ```
> 
> I hope this helps (:

Why did you use

```auto

DECLARE_DYNAMIC_MULTICAST_DELEGATE

```

over

```auto

DECLARE_EVENT

```

?

DECLARE\_DYNAMIC\_MULTICAST\_DELEGATE is very often used in the gameplay framework so I am wondering why I would use it instead of DECLARE\_EVENT.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9cf15cf7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9cf15cf7](https://forums.unrealengine.com/u/anonymous_user_9cf15cf7)\
**Post date:** [July 3, 2015, 5:14pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/7 "2015-07-03T17:14:59Z")

</div>

Hello,

This is what worked

```auto

#include "Acpp_character.h" // to get the class declaration

void Acpp_tennisBall::BeginPlay ()
{
    Acpp_character *c = Cast <Acpp_character>(GetWorld ()->GetFirstPlayerController ()->GetCharacter ());
    c->OnMoveToPosition.AddDynamic (this, &Acpp_tennisBall::MoveToPosition);
}

```

Is there a elegant way to do this via ConstructorHelpers. The Acpp\_character object is instantiated only ingame through the custom GameMode’s Default Pawn Class.

---

<div class="post-metadata">

**Author:** ![yateam](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@yateam](https://forums.unrealengine.com/u/yateam)\
**Post date:** [July 3, 2015, 8:42pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/8 "2015-07-03T20:42:28Z")

</div>

Do I need to remove delegates (REmoveDynamic) when the actor is destroyed? How does that work with UE garbage collection management?

---

<div class="post-metadata">

**Author:** ![Henry\_Read](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Henry\_Read](https://forums.unrealengine.com/u/Henry_Read)\
**Post date:** [July 6, 2015, 7:42am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/9 "2015-07-06T07:42:09Z")

</div>

It is actually dynamic delegates. If you mark BlueprintAssignable it will appears as blueprints’ eventdispatcher

---

<div class="post-metadata">

**Author:** ![LuLima](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LuLima](https://forums.unrealengine.com/u/LuLima)\
**Post date:** [March 4, 2016, 1:33am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/10 "2016-03-04T01:33:19Z")

</div>

I’m having trouble binding methods from other objects. Can someone help me?

---

<div class="post-metadata">

**Author:** ![manoelneto](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@manoelneto](https://forums.unrealengine.com/u/manoelneto)\
**Post date:** [March 4, 2016, 2:22pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/11 "2016-03-04T14:22:34Z")

</div>

> [@yateam;324087](#):
>
> Do I need to remove delegates (REmoveDynamic) when the actor is destroyed? How does that work with UE garbage collection management?

The subscription to a delegate is a weak reference, so you don’t have to worry about them keeping objects from being garbage collected.

---

<div class="post-metadata">

**Author:** ![LuLima](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LuLima](https://forums.unrealengine.com/u/LuLima)\
**Post date:** [March 4, 2016, 2:54pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/12 "2016-03-04T14:54:01Z")

</div>

Can someone show how is it done? Bind a Delegate of one Actor to another? I’m trying but the game keeps crashing. Can anyone supply a functional example code? Please?

---

<div class="post-metadata">

**Author:** ![manoelneto](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@manoelneto](https://forums.unrealengine.com/u/manoelneto)\
**Post date:** [March 6, 2016, 12:19am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/13 "2016-03-06T00:19:01Z")

</div>

> [@LuLima;486760](#):
>
> Can someone show how is it done? Bind a Delegate of one Actor to another? I’m trying but the game keeps crashing. Can anyone supply a functional example code? Please?

Something like this:

```auto

void AMyActor::MyFunction(AActor* AnotherActor)
{
   // When AnotherActor is destroyed, call this->HandleAnotherActorDestroyed()
   AnotherActor->OnDestroyed.AddDynamic(this, &AMyActor::MyHandle);
}

void AMyActor::HandleAnotherActorDestroyed()
{
   // Do something here
}

```

---

<div class="post-metadata">

**Author:** ![astonish](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@astonish](https://forums.unrealengine.com/u/astonish)\
**Post date:** [March 8, 2016, 9:17pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/14 "2016-03-08T21:17:04Z")

</div>

> [@maikklein;323735](#):
>
> Why did you use
> 
> ```auto
> 
> DECLARE_DYNAMIC_MULTICAST_DELEGATE
> 
> ```
> 
> over
> 
> ```auto
> 
> DECLARE_EVENT
> 
> ```
> 
> ?
> 
> DECLARE\_DYNAMIC\_MULTICAST\_DELEGATE is very often used in the gameplay framework so I am wondering why I would use it instead of DECLARE\_EVENT.

It’s been a while since I did this, but both from my experience using the delegates and looking at their usage in the code base the big difference between DECLARE\_EVENT and DECLARE\_DYNAMIC\_MULTICAST\_DELEGATE is that dynamic delegates can be looked up by name and therefore can be used/exposed to blueprints. I don’t think I’ve ever been able to use DECLARE\_EVENT from a blueprint.

---

<div class="post-metadata">

**Author:** ![LuLima](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LuLima](https://forums.unrealengine.com/u/LuLima)\
**Post date:** [March 9, 2016, 8:50pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/15 "2016-03-09T20:50:55Z")

</div>

> [@manoelneto;487616](#):
>
> Something like this:
> 
> ```auto
> 
> void AMyActor::MyFunction(AActor* AnotherActor)
> {
> // When AnotherActor is destroyed, call this->HandleAnotherActorDestroyed()
> AnotherActor->OnDestroyed.AddDynamic(this, &AMyActor::MyHandle);
> }
> 
> void AMyActor::HandleAnotherActorDestroyed()
> {
> // Do something here
> }
> 
> ```

I tried something like this and it is crashing during gameplay. how to you define the delegate?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2ac5051c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2ac5051c](https://forums.unrealengine.com/u/anonymous_user_2ac5051c)\
**Post date:** [May 21, 2016, 5:25am UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/16 "2016-05-21T05:25:49Z")

</div>

## OnTestDelegate.Broadcast();

How could i do this in blueprint?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f82846a8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f82846a8](https://forums.unrealengine.com/u/anonymous_user_f82846a8)\
**Post date:** [July 3, 2016, 3:42pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/17 "2016-07-03T15:42:50Z")

</div>

![9e1352c74053c17b14d524d3af5f478f771a1aa7.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/9/e/9e1352c74053c17b14d524d3af5f478f771a1aa7.png) ![checkpoint.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/7/b/47b92b0b12ccdd6d149305cd2b8c6325500a108d.png)

Hi !  
I not sure if this is the right place to ask this question, so please guide me if it is not.

Current I am doing the “Time Attack Racer” example, I wrote the ‘checkpoint’ entirely in C++ while the other actors in blueprint (Cause I need the checkpoint to do other things for my project).But I am Stuck at the Event dispatcher, I tried the above ’ event dispatch’ method in my checkpoint and cannot bind it to ‘Tracker’ in the blueprint.  
Any Help is appreciated !  
Thank 😉

cheer

---

<div class="post-metadata">

**Author:** ![Mr.tellino](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr.tellino](https://forums.unrealengine.com/u/Mr.tellino)\
**Post date:** [February 20, 2017, 10:59pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/18 "2017-02-20T22:59:54Z")

</div>

> [@eXi;240068](#):
>
> ```auto
> 
> #pragma once
> 
> #include "GameFramework/PlayerController.h"
> #include "MyPlayerController.generated.h"
> 
> /**
> * 
> */
> 
> DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTestDelegate);
> 
> UCLASS()
> class SOCKETTEST_API AMyPlayerController : public APlayerController
> {
> GENERATED_UCLASS_BODY()
> 	
> 	
> public:
> virtual void Tick(float DeltaTime) override;
> virtual void BeginPlay() override;
> 
> void TestFunction();
> 
> UPROPERTY(BlueprintAssignable, Category = "Test")
> FTestDelegate OnTestDelegate;
> };
> 
> ```

Hi, fairly new UE user here.  
I noticed making the delegate function private also works. Is there any reason for it to be public instead of private?

Thanks!

---

<div class="post-metadata">

**Author:** ![eXi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/exi/32/906583_2.png) [@eXi](https://forums.unrealengine.com/u/eXi)\
**Post date:** [February 21, 2017, 12:18pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/19 "2017-02-21T12:18:20Z")

</div>

> [@bloodmeat10;669553](#):
>
> Hi, fairly new UE user here.  
> I noticed making the delegate function private also works. Is there any reason for it to be public instead of private?
> 
> Thanks!

If it works with them being private, then no.  
Have you tested binding the Delegate in a different BP Class then “MyPlayerController”?  
Blueprint exposed UPROPERTY’s always tell me they can’t be private. So I thought protected and public is better here :x

---

<div class="post-metadata">

**Author:** ![Nico\_de\_Poel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nico\_de\_Poel](https://forums.unrealengine.com/u/Nico_de_Poel)\
**Post date:** [February 21, 2017, 12:36pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/20 "2017-02-21T12:36:13Z")

</div>

> [@eXi;669865](#):
>
> If it works with them being private, then no.  
> Have you tested binding the Delegate in a different BP Class then “MyPlayerController”?  
> Blueprint exposed UPROPERTY’s always tell me they can’t be private. So I thought protected and public is better here :x

You can add “meta = (AllowPrivateAccess = true)” to the UPROPERTY macro on a private field, which explicitly gives Blueprint permission to read and write that field. For dynamic multicast delegates I prefer to keep the field private, apply AllowPrivateAccess, and provide a public read-only accessor to the delegate for C++ code to bind to it. That way users of your code can’t accidentally overwrite the delegate with a different instance.

---

<div class="post-metadata">

**Author:** ![DaGaffer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DaGaffer](https://forums.unrealengine.com/u/DaGaffer)\
**Post date:** [February 25, 2017, 8:44pm UTC](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185/21 "2017-02-25T20:44:38Z")

</div>

Very useful post here. Having just implemented a C+±declared Event Dispatcher per the above - a note, for full functionality you will want to add BlueprintCallable as well to allow your blueprint to call your event dispatcher:

\> UPROPERTY(BlueprintAssignable, **BlueprintCallable** , Category = “Test”)  
\> FTestDelegate OnTestDelegate;

[Next page](https://forums.unrealengine.com/t/c-event-dispatchers-how-do-we-implement-them/23185.md?page=2)
