Original Author: ()
In this wiki I share with you the core code that you need to implement for a variety of delegates in UE4!
A delegate is basically an event that you can define and call and respond to.
Every time the event is fired off, anyone who is listening for this event will receive it and be able to take appropriate action.
In the case of multicast delegates, any number of entities within your code base can respond to the same event and receive the inputs and use them.
In the case of dynamic delegates, the delegate can be saved/loaded within a Blueprint graph (they're called Events/Event Dispatcher in BP).
For my example I will be using exclusively DYNAMIC_MULTICAST which is the type that is most useful in Blueprints :)
You create the signature of the delegate, which declares what inputs any receiving functions should specify.
//RamaMeleeWeapon class .h DECLARE_DYNAMIC_MULTICAST_DELEGATE_SixParams( FRamaMeleeHitSignature, class AActor*, HitActor, class UPrimitiveComponent*, HitComponent, const FVector&, ImpactPoint, const FVector&, ImpactNormal, FName, HitBoneName, const struct FHitResult&, HitResult );
Notice the macro declares that I will be adding 6 parameters, there are similar macros for other quantities of parameters :)
You call the delegate within the class structure where it was defined, making sure to only execute it if it is currently bound, meaning at least 1 entity is listening for this delegate / event.
//.h //RamaMeleeWeapon class .h //This should be in the class which calls the delegate, and where the signature was defined //This is an instance of the signature that was defined above! FRamaMeleeHitSignature RamaMeleeWeapon_OnHit;
//.cpp //Only the code that is supposed to initiate the event calls Broadcast() if(RamaMeleeWeapon_OnHit.IsBound()) // Add Event and see your new delegate\! So nice\! This is an additional benefit of using DYNAMIC\_MULTICAST delegates\! Multi-cast implies binding multiple of various object instances to the delegate and then firing off the event to everyone from a single .Broadcast, which can include your Level Blueprint as a recipient/listener\! ## Video Example Here is a video on how a C++ delegate created in an actor component in C++ looks and is called in Blueprints\! The code in this wiki and this video are from my [Melee Weapon Plugin](http://ue4code.com/melee_weapon_system_plugin_per_bone_collision_accuracy) [https://youtube.com/http://www.youtube.com/watch?v=aufEB4TCf30&t=5m24s](https://youtube.com/http://www.youtube.com/watch?v=aufEB4TCf30&t=5m24s) [Skip to 5:24](http://www.youtube.com/watch?v=aufEB4TCf30&t=5m24s) ## Further Reading Epic Documentation: ## DYNAMIC\_MULTICAST And Other Types There are other delegate types besides DYNAMIC\_MULTICAST that are not quite as versatile when it comes to Blueprints. Check out the source code of Delegate.h ` Runtime/Core/Public/Delegates/Delegate.h` For a detailed explanation\! Sample from this file: ```cpp ** * C++ DELEGATES * ----------------------------------------------------------------------------------------------- * * This system allows you to call member functions on C++ objects in a generic, yet type-safe way. * Using delegates, you can dynamically bind to a member function of an arbitrary object, * then call functions on the object, even if the caller doesn't know the object's type. * * The system predefines various combinations of generic function signatures with which you can * declare a delegate type from, filling in the type names for return value and parameters with * whichever types you need. * * Both single-cast and multi-cast delegates are supported, as well as "dynamic" delegates which * can be safely serialized to disk. Additionally, delegates may define "payload" data which * will stored and passed directly to bound functions.
Enjoy using delegates in UE4 so that any part of your code base can respond to an event triggered by one section of your code!
Also enjoy exposing delegates via C++ for the rest of your team to use in Blueprints!