Delegates
- Delegates can call member functions on C++ objects in a generic, type-safe way ... bound dynamically to a member function of an arbitrary object ... even if the caller does not know the object's type. Delegates are safe to copy; passing by value allocates on the heap; pass by reference whenever possible.
- Three types: Single; Multicast; Dynamic (UObject, serializable).
- Signatures support return values, const functions, up to four "payload" variables, up to eight function parameters. Macros: DECLARE_DELEGATE, _OneParam, _RetVal, etc.; variations DECLARE_MULTICAST_DELEGATE, DECLARE_DYNAMIC_DELEGATE, DECLARE_DYNAMIC_MULTICAST_DELEGATE.
UDELEGATE(BlueprintAuthorityOnly)applies UFunction specifiers. - Declarations may exist at global scope, namespace, or within a class declaration, not within a function body.
- Binding: Bind, BindStatic, BindRaw (raw pointers: calling Execute after deleting the target is unsafe), BindLambda, BindSP (weak reference), BindUObject (weak reference), UnBind. If bound to UObject or shared pointer, the delegate keeps a weak reference ... handle via IsBound / ExecuteIfBound.
- Payload data passed at bind-time; all types except dynamic support payloads.
MyDelegate.BindRaw(&MyFunction, true, 20); - Execute without checking; ExecuteIfBound; IsBound. Executing an unbound delegate could scribble over memory in some instances; calling Execute() before binding triggers an assertion.