Delegates🔗
You must first declare a delegate type to indicate what parameters and return value your delegate wants.
In global scope:
From there, you can pass around values of your delegate type, bind them, and execute them:
Note: A
delegate
declaration is equivalent to aDECLARE_DYNAMIC_DELEGATE()
macro in C++. Functions bound to delegates are required to be declared asUFUNCTION()
.
Events🔗
Events are similar to delegates, but can have multiple functions added to them, rather than always being bound to only one function.
Declare events with the event
keyword in global scope, then use AddUFunction()
and Broadcast()
:
Note: An
event
declaration is equivalent to aDECLARE_DYNAMIC_MULTICAST_DELEGATE()
macro in C++. Functions added to events are required to be declared asUFUNCTION()
.
Events in Blueprint🔗
By declaring OnExampleEvent
as a UPROPERTY()
in the previous example, we allow it to be accessed from blueprint.
For events this means it will appear in the Event Dispatchers
list for actors in the level, and we can bind it from the level blueprint:
Tip: Automatic signature generation in Visual Studio Code🔗
If you bind a delegate or add a function to an event, and the function does not exist yet, the visual studio code extension will try to offer to create it for you.
Click the lightbulb icon or press Ctrl + ., and select the Generate Method
option from the code actions dropdown: