Learning Unreal 5: How to Communicate using Event Dispatchers

Unreal Engine event dispatchers are one of three main ways that you can communicate between blueprints. For anyone familiar with observers or publishers & subscribers, it’s the same concept. For anyone else, event dispatchers are essentially a call and response setup. Imagine a medieval town. A lookout keeps it’s eye out for trouble. One day, they spot it and call out an alarm. Inside the city, guards muster towards the gates, townsfolk retreat to safe havens. Outside, the brigands that came near, triggering the guard’s call, hear that call and retreat to the edge of the forest having lost the element of surprise.

In this example, the lookout is the Publisher. The part of the event dispatcher that performs the call. The others – guards, townsfolk, brigands – they’re all observers or subscribers. They have pre-planned responses to hearing that call. Their actions differ, but it’s all triggered by the lookout’s call.

An important note for this as well – the Lookout doesn’t have any knowledge of the people reacting to them, but the guards, townsfolk, and brigands are aware and ready to respond to the Lookout. Likewise in blueprints, the publisher doesn’t know about the subscribers. This creates loosely coupled code, which is a cleaner design pattern than making direct references between two blueprints all the time. Following this pattern, you can avoid issues that may arise by having connections between blueprints – either that make code messier and harder to manage, or wind up causing errors by having things referenced in memory that don’t need to be which can potentially lead to memory issues.

Lets look at how to set up Unreal Engine event dispatchers in an example.

Toggling Lights with Event Dispatchers

This section uses an Interface to help with object interaction. Interfaces are another way to communicate with blueprints. So, we’ll need to set up the interface and keybinds. You can skip this if you instead set up the button in the next section to be triggered on On Overlap with a box collision.

Create and set up an interact interface (optional)

This sets up a blueprint interface to facilitate the interaction part of the button press in the next section, then connects it to a keybind.

  1. Create the interface. Create a new Blueprint Interface by right clicking the content drawer Blueprints -> Blueprint Interface. Name it BPI_InteractInterface.
  2. Set up the interface. Add an On Interact function. Save and compile.
  3. Set up the keybind for interacting. In your player controller, right click the event graph and select an input (ie, E key).
  4. Connect that to a Line Trace by Channel node.
    • Start Input: Create a node for your camera, in my case it was First Person Camera. From that node, drag and create a Get World Location node. Drag that node to the Start input of Line Trace By Channel.
    • End Input: From the First Person Camera node, drag out again and create a Get Forward Vector node. From this node, drag out and create a Multiply node. For the second input into Multiply, create a Make Literal Float node and connect. Drag the output from Multiply and create an Add node. Use the Get World Location node as a second input to the add. Finally, connect Add to the End input on Line Trace By Channel node.
  5. Drag from Line Trace By Channel and create a Branch node.
    • From the Line Trace By Channel node, drag from the return value to the red condition input of the Branch node.
  6. From the True connection of the Branch node, drag and create an On Interact node from BPI_InteractInterface.
    • From Line Trace By Channels drag from the Out hit pin and create a Break Hit Result node. Expand the Break Hit Result node and drag the Hit Actor to the Target of On Interact.
Visual representation of the output of the steps to connect the Interact Interface to a keybind.
How the connection of keybind to on interact setup should appear in the Player Controller event graph.

Set up the button

  1. Create a new Blueprint Actor named BP_Button.
  2. Under components on the left, click Add and add a static mesh or shape. For example purposes, I just selected Cube.
  3. Create a new Event Dispatcher. In the bottom left of the default layout, there’s an Event Dispatchers section. Create a new event dispatcher and call it OnButtonPress.
Screenshot demonstrating the BP_Button blueprint's event graph calling the OnButtonPress event dispatcher.
How the setup of the Event Dispatcher call should look in the BP_Button event graph.

Connect the interact interface (only necessary if you set up the interface in earlier steps)

  1. In BP_Button, go to the top middle and click on the Class Settings button. In the Details Panel to the right, go to interfaces and add BPI_InteractInterface.

Set up the light

  1. Create a new blueprint actor and name it BP_Light.
  2. Under Components, add a new Point Light.
  3. In the event graph, drag from Event Begin Play and create a Get Actor of Class node. In the Actor Class dropdown, find and select the BP_Button blueprint.
  4. From Get Actor of Class node, drag the connection and create a new Bind On Button Press. Make sure the Return Value of the Get Actor nod connects to the Target of the Bind event node.
    • Alternatively, you can search for Assign On Button Press. This will create both a Bind Event node and a Custom Event node.
  5. Drag from Bind Event node’s Event pin and create a new custom event. Name this custom event “Toggle Light”.
  6. Drag from ToggleLight’s connection pin and create a Flip flop node. Drag the Point Light component from under variables onto the event graph.
    • From the point light node, drag and create a Set intensity node. Set the intensity to 10,000 or higher. In the Flip Flop node, drag a connection from A to the Set Intensity node.
    • Copy the Set Intensity and Point Light nodes and paste below. Connect the new Set Intensity node to the B connection of the Flip Flop node. Set that intensity to 0.0.
Visual example of the end result for the BP_Light event graph.
How the BP_Light setup should look

Add the components to the level

  1. Drag BP_Light and BP_Button into the level editor and place somewhere you can see them easily.
  2. Save and compile everything, then run. You should now be able to interact with the button to toggle the light on and off.

Other Resources

For further examples and alternate explanations, check out the following:

One Reply to “Learning Unreal 5: How to Communicate using Event Dispatchers”

Comments are closed.