# (UE5.3) how to bind to multicast delegate in C++?

**URL:** <https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, UE5, delegate, question, CPP\
**Created:** [December 19, 2023, 2:10pm UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219 "2023-12-19T14:10:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CyFio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CyFio](https://forums.unrealengine.com/u/CyFio)\
**Post date:** [December 19, 2023, 2:10pm UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/1 "2023-12-19T14:10:35Z")

</div>

I declared an dynamic multicast delegate in C++ as:

```c++
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMsgDispatcher, const struct FRecvContext&, recvContext);
UClass
class UXXComponent: : public UActorComponent
{
	GENERATED_BODY()
public:
	UPROPERTY(BlueprintAssignable)
	FMsgDispatcher OnRecvMsg;
}

```

I can assign event to it in BP like:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/1/c/a1caad9a44dd059c34fdbc0c0769b7c3776a9ae3.png)  
But I dont know the proper way to add any delegate in CPP, it seems that I should call Add() or AddUnique(), but the parameter required is confusing me. It requires ‘TScriptDelegate’ which I cannot figure out how to construct.  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/d/2/ed2904accd121d4ca1a4215bdcacb5b16b54cf46.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/a/d/aaddabfd5c489fd2f2bad59663202d6edb63a8c7.png)

Does anyone know how to add c++ function to multicast delegate? The official site only shows examples on single delegate binding.

---

<div class="post-metadata">

**Author:** ![xenofsnstudios](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xenofsnstudios/32/1194578_2.png) [@xenofsnstudios](https://forums.unrealengine.com/u/xenofsnstudios)\
**Post date:** [December 19, 2023, 3:13pm UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/2 "2023-12-19T15:13:50Z")

</div>

Use `AddDynamic`.

Ex.  
`OnRecvMsg.AddDynamic(this, &ASomeClass::OnSomeFunction);`

---

<div class="post-metadata">

**Author:** ![IrSoil](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IrSoil](https://forums.unrealengine.com/u/IrSoil)\
**Post date:** [December 19, 2023, 3:56pm UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/3 "2023-12-19T15:56:48Z")

</div>

For dynamic delegates there is a **macro** named `AddDynamic` with params `ContextObject`(usually passing `this`) & `UFunction pointer` (usually passed like `&ASomeClass::OnSomeFunction` as said in other comment)

But. If your delegate need no being exposed to bp, then it’s better to use native (non-dynamic) delegates, as they have way more ways to interact with cpp. So your code will looks like:

```c++
//header
DECLARE_MULTICAST_DELEGATE_OneParam(FMsgDispatcherNative, const struct FRecvContext&);

UClass
class UXXComponent: : public UActorComponent
{
	GENERATED_BODY()
public:
	UPROPERTY(BlueprintAssignable)
	FMsgDispatcherNative OnRecvMsg;
}

//cpp
void UXXComponent::someFunction()
{
     OnRecvMsg.AddLambda([](const struct FRecvContext& Context){
         //some actions with `Context`
     });

    FRecvContext Context = ... ;
    OnRecvMsg.Broadcast(Context);
}

```

If you need both types of delegates then usually i’m define both types and call broadcasts next to each other

---

<div class="post-metadata">

**Author:** ![CyFio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CyFio](https://forums.unrealengine.com/u/CyFio)\
**Post date:** [December 21, 2023, 7:10am UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/4 "2023-12-21T07:10:13Z")

</div>

It seems that UE5.3 does not have AddDynamic macro, I can only find a similar signature like this  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/3/6/236a1d3c8a77d398a036e76d4acfb64dadf857fc.png)

is this the same as the AddDynamic()?

---

<div class="post-metadata">

**Author:** ![SolidSk](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/solidsk/32/1194513_2.png) [@SolidSk](https://forums.unrealengine.com/u/SolidSk)\
**Post date:** [December 21, 2023, 7:45am UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/5 "2023-12-21T07:45:00Z")

</div>

This is an “autocompletion” problem.  
Use **AddDynamic**.

[https://docs.unrealengine.com/5.3/en-US/dynamic-delegates-in-unreal-engine/](https://docs.unrealengine.com/5.3/en-US/dynamic-delegates-in-unreal-engine/)

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [December 21, 2023, 8:10am UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/6 "2023-12-21T08:10:11Z")

</div>

![dyn](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/8/51874c0d155079685683b19c4361babea2fcb8e6.png)  
Can confirm that AddDynamic works & compiles no problem.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [February 20, 2024, 11:11am UTC](https://forums.unrealengine.com/t/ue5-3-how-to-bind-to-multicast-delegate-in-c/1538219/7 "2024-02-20T11:11:15Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
