Recently I had to deep dive into the delegate macros of Unreal Engine. While I was trying to understand how the pieces fit together, I was making a bunch of notes for myself. I’ll share the notes here for future reference for myself and for anyone else who is suffering from the same task as me.
DECLARE_DYNAMIC_MULTICAST_DELEGATE creates a new TBaseDynamicMulticastDelegate class with the delegate name.
TBaseDynamicMulticastDelegate is a TMulticastScriptDelegate ----->>> FMulticastScriptDelegate
TBaseDynamicDelegate is a TScriptDelegate ----------------------->>> FScriptDelegate
AddDynamic macro uses TBaseDynamicMulticastDelegate::__Internal_AddDynamic
__Internal_AddDynamic boils down to TMulticastScriptDelegate::Add which takes TScriptDelegate (init via __Internal_BindDynamic)
__Internal_BindDynamic assigns user object and function name --- same as BindUFunction.
IMPORTANT: Dynamic delegates do not actually store function reference! Only user object + function name.
- Also it seems like we can bind function without the necessary params.
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE creates a new TSparseDynamicDelegate !! AND !! TBaseDynamicMulticastDelegate
TSparseDynamicDelegate is a FSparseDelegate
AddDynamic macro uses TSparseDynamicDelegate::__Internal_AddDynamic
__Internal_AddDynamic boils down to TSparseDynamicDelegate::Add which takes FScriptDelegate (init via __Internal_BindDynamic)
__Internal_BindDynamic assigns user object and function name --- same as BindUFunction.
TSparseDynamicDelegate::Add boils down to FSparseDelegateStorage::Add with delegate owner, name, and the FScriptDelegate.
TSparseDynamicDelegate::Add also sets the "bIsBound" state of the sparse delegate.
IMPOTANT: If "bIsBound" state not set, the sparse delegate wont broadcast.
The "bIsBound" param is protected -----> we can use FSparseDelegate::__Internal_AddUnique
FMulticastDelegateProperty is the property that is exposed to the editor.
Its also what gets created when we make an event dispatcher in blueprint.
We can bind to this using FindPropertyByName("MyDelegate")->AddDelegate