Event Dispatchers

An Event Dispatcher allows a Blueprint to inform us when an Event happens. The Level Blueprint and other Blueprint classes can listen to this Event, and they may have different Actions that run when the Event is triggered.

We create Event Dispatchers in the My Blueprint panel. As an example, let's create a Blueprint named BP_Platform. When an Actor overlaps the BP_Platform Blueprint, it calls an Event Dispatcher called PlatformPressed. The Level Blueprint is listening for the PlatformPressed Event and spawns an explosion when this Event is triggered:

  1. Create or use an existing project that contains the starter content.
  2. Create a Blueprint and use Actor as the parent class. Name it BP_Platform and open it in the Blueprint Editor.
  1. Click the Add Component button in the Components panel and choose the Static Mesh Component. In the Details panel, choose the Shape_Cylinder Static Mesh and change the Z value of the Scale attribute to 0.1. Also, change Collision Presets to OverlapAllDynamic, as shown in the following screenshot:

  1. Compile the Blueprint. In the My Blueprint panel, create an Event Dispatcher and name it PlatformPressed. An Event Dispatcher can have input parameters. Let's create one to send a reference to the BP_Platform instance that was overlapped. In the Details panel, create a new parameter in the Inputs category, name it BP_Platform, and use it as a BP Platform type object reference, as shown in the following screenshot:

  1. Right-click Event Graph and add Event ActorBeginOverlap. Drag the PlatformPressed Event Dispatcher and drop it in Event Graph. Choose Call in the submenu. Right-click Event Graph, search for self, and select the Get a reference to self Action. The Self Action returns a reference to the current instance. Connect the Actions, as shown in the following screenshot:

  1. Compile the Blueprint. In the Level Editor, drag BP_Platform from Content Browser and drop it in the Level to create an instance. Click the Blueprint button at the top of the Level Editor and choose Open Level Blueprint. Right-click Event Graph and select Add Platform Pressed, as shown in the following screenshot:

  1. Right-click Event Graph, search for spawn actor, and select Spawn Actor from Class. Click the drop-down menu in the Class parameter and select Blueprint Effect Explosion. Drag from the blue pin of the PlatformPressed (BP_Platform) Event, drop it in the graph in order to open Context Menu, and choose the GetActorTransform Action. Connect the nodes, as shown in the following screenshot:

  1. Compile the Level Blueprint and click the Play button of the Level Editor to test the Level. Move your character to the location where BP_Platform is. When your character overlaps it, the Level Blueprint will spawn an explosion at the same place.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset