In UnrealEngine, UFUNCTION
is used for enriching functions with additional specifiers for blueprint usage, replication and delegates.
However, some delegate types don't seem to allow to bind a UFUNCTION
(like a multicast delegate), while other types require to bind a UFUNCTION
(like a dynamic multicast delegate).
Is there an overview available, what delegate type accepts which type of function (normal c++ or UFUNCTION
)?
Only dynamic delegates require the functions which can be bounded to be a UFUNCTION
.
+-----------------------------------------------+----------------------+--------------------+
| Type | binds c++ function | binds `UFUNCTION` |
+-----------------------------------------------+----------------------+--------------------+
| Singlecast | yes | yes |
| Multicast | yes | no |
| Event | yes | ? |
| Dynamic singlecast | no | yes |
| Dynamic multicast | no | yes |
| `FTimerDelegate` (singlecast) | yes | yes |
| `FTimerDynamicDelegate` (dynamic singlecast) | no | yes |
+-----------------------------------------------+----------------------+--------------------+
(This is my observation so far. In case of errors, please comment or edit or add an answer.)
UFUNCTION
increases compile time and artifact size, so only use the macro when required by the calling code.
Dynamic delegates support serializing, have additional code for working in Blueprint graphs (called Events/Event Dispatcher in BP) and are slower than the other delegate types. If you only need delegates for C++, you don’t need dynamic ones most of the time.
Delegates which accepts binding of c++ functions, can be wrapped by a template
template<typename T>
struct MyTemplateWrapper
{
DECLARE_MULTICAST_DELEGATE_OneParam(FMyDelegateWithTemplate, T);
};
Use it like MyTemplateWrapper<float>::FMyDelegateWithTemplate MyCallback;
.
Keep in mind: The UPROPERTY
macro is not supported for MyCallback
since MyTemplateWrapper
can't be a USTRUCT
/UCLASS
(since they don’t support templates). However, because the non dynamic delegate types don't support blueprints nevertheless, the missing UPROPERTY
is not a loss.
UFUNCTION
Short summary at unreal answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With