[Plugin] Event Aggregator (Global Event Handler) for Blueprints and C++

LINK TO THE MARKETPLACE: https://www.unrealengine.com/marketp…andler-for-ue4

Hey guys, i’ve been developing a plugin for Unreal Engine, to allow using the event aggregator pattern in your projects. Inspired in my journeys trough .NET lands (using frameworks like Prism and Caliburn Micro) i ended up realizing that Unreal could really benefit from an event aggregator that is easy to use (specially as easy to use as the Caliburn Micro’s event aggregator).

For all the people that doesn’t know what an event aggregator is: An event aggregator pattern allows to anonymously communicate and send messages (payloads) from a Publisher object to one or more Subscriber objects. The publisher doesn’t need to know who is listening the message (if any) and the subscriber object doesn’t need to know either who is sending that message.

In Unreal Engine, this is very advantageous since the only method we have to communicate anonymously is the UInterface, and it doesn’t really work in an anonymous way since we still need the raw reference of an object (raw=not casted) for sending a message. Obtaining that reference could be complicated sometimes, we could get that reference using “GetAllActorsOfClass” (which is slow and also adds a direct dependency to the class) or we could expose an Actor variable in the view-port and link the reference there (which doesn´t work in level streaming scenarios unless soft references are used, also doesn´t work when Actors are spawned in runtime).

So, what my plugin does to “fix” that is to allow sending messages between objects (UObjects, Actors, etc) in an anonymous way. You could send a message to hundreds of different Actors (Of different types) without even knowing if they exist.To better explain how it works, i will show you the current workflow (note that some things may change in the final version):

# First: We need to create event objects (which will act as signatures for filtering who receives the messages).

1.jpg
Additionally, we could create variables inside these event objects, so we could also use the instances of the events as payloads (extra content of the message) as well, although any UObject could be a payload.

# Second: We publish a message in the Publisher UObject/Actor


As you see, i´m using a Subsystem (GameInstance based) to invoke the Publish event. The “class to listen” is the class of one of the events created in the first step. Any Object subscribed to that event class will receive the message. The Payload and Metadata is optional. And as i mentioned before, the payload could be the reference of any UObject, although i prefer to use an instance of the same Event (Event A) as payload. The instance of the payload can be created using the construct object method, as shown below.

Also, i plan to also give the option to use a LocalPlayerSubsystem and WorldSubsystem as well, to publish and subscribe messages in those contexts and allow for a better flexibility using the system.

# Third: Subscribe and Listen for messages.


In the subscriber, the only thing needed is to call the Subscribe() method, link it to a Custom Event and that´s it! The custom event fill be fired whenever an object publish a message to EventA.
Notice that i´m not using components or interfaces to achieve this functionality. Just two methods, pretty easy!

Of course, there´s also methods for unsubscribing, debugging, etc. Unsubscribing is also made automatically if the subscribed objects were destroyed.


Additionally i plan to take advantage of the UE framework and to give more options on the publish/subscribe method. For example: publish a message to EventA that will only be received by Objects of type Actor, that implements InterfaceA and has ComponentB attached. Although i´m not sure if the last part will be shipped, since it can be done by using the payloads and branching on the subscriber and i´m not sure yet how it will affect the performance of the plugin. Although currently, the system is lightning fast.

Also, notice how in the example the only dependency of both the publisher and subscriber is the “EventA” class. This is pretty nice since without the direct dependencies, the objects are now more reusable and testable as well!


For finishing, i plan to submit this plugin in a few days. **And i plan to release it for free in the first days. **Although i´m not sure if this is possible to do in the marketplace, i will be sending a message to the staff to confirm if it can be released for free temporarily.

I will be updating when the plugin is going to be released so you can get it for free. I only ask you guys to give my plugin a chance and try to review it in the marketplace (but please take into account that the plugin may have issues that will be fixed in the first days with the help of your feedback ;p). However, you´re not obligated to review my plugin, so don´t feel bad if you´re not a reviewer type of person =D.

So guys, hopefully you liked my post. If you have any feedback or question, please comment below!

Hey guys, the plugin has been released and can be grabbed for free temporarily.

That’s fantastic, thank you! I will check it out now!

Thanks for this!