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

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

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:


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.

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

1 Like

Use AddDynamic.

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

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:

//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

It seems that UE5.3 does not have AddDynamic macro, I can only find a similar signature like this
image

is this the same as the AddDynamic()?

This is an “autocompletion” problem.
Use AddDynamic.

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

2 Likes


Can confirm that AddDynamic works & compiles no problem.

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