Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use UFUNCTION for delegates

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)?

like image 496
Roi Danton Avatar asked Sep 14 '25 09:09

Roi Danton


1 Answers

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.)

Performance

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.

C++ template support for non dynamic delegates

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.

Off topic: Additional usage of UFUNCTION

Short summary at unreal answers.

like image 113
Roi Danton Avatar answered Sep 17 '25 19:09

Roi Danton