Pages

segunda-feira, 17 de novembro de 2014

Using Event Dispatcher in Blueprints

Blueprints has a feature called "Event Dispatcher" that allows a type of communication between Class Blueprints and Level Blueprint. In this article we will modify the example used in the previous article and create a blueprint that represents a detonator that will be responsible for initiating the explosion.

The Blueprint "Detonator" that we will create in this article has a variable "Time" with the time remaining to the explosion. When an actor touches the blueprint it will be activated and begins the countdown to the explosion. When the variable "Time" is zero, the Blueprint "Detonator" will generate an event called "Detonate" using Event Dispatcher.

This ends the mission of the "Detonator" Blueprint. The way in which the explosion occurs is not responsibility of the "Detonator" Blueprint. Thus we have a blueprint that can be used in various situations.

The explosion is created on the Level Blueprint when the "Detonate" event of the "Detonator" Blueprint is generated.

Create a new Blueprint named "Detonator" based on the Actor class. Add these two variables:

- Time: Integer type, editable and default value of 10.
- Active: Boolean type, not editable and default value of "false" (unchecked).

Switch to the Components mode. The "Detonator" Blueprint is visually represented by two components: a Box and a Text Render. Click the "Add Component" button and first choose the "Box" and then the "Text Render":


Click the Box component that was created and uncheck the "Hidden in Game" option which is in the "Rendering" category for it to appear during the game.

Click the TextRender component created and rename it to "Display". In the "Text" category, in the "Horizontal Alignment" property choose "Center". In the "Text Render Color" property choose a red color as the image below:


The Blueprint will look like:


The value of 10 being displayed was obtained from the "Time" variable in the Construction Script:


Switch to the Graph mode and create a new "EventDispatcher" with the name of "Detonate":


The detonator will be activated when someone touch it, so we'll use the event "Actor Begin Overlap". The "Active" variable is used so that the detonator is activated only once. It also uses a Timer and a Custom Event that is responsible for the countdown of the detonator. The script became like this:


The "Set Timer Delegate" function is just a variation of the "Set Timer" function, but instead of writing the name of the Event, we used the red pin of the custom event to make the association with the "Delegate" parameter of the "Set Timer Delegate" function. The example above just means that the "Clock" event will be called every 1 second.

The "Clock" event is responsible for decrease in one the "time" variable, update the text of the TextRender component and to verify if the "time" variable is equal to zero in order to clear the Timer and call the Event Dispatcher "Detonate":

Click to enlarge

The "Clear Timer" action disables a Timer. You must provide the name of the Custom Event that is associated with the Timer. In the above example we deactivate the timer to stop the counter at zero.

The action "Call Detonate" generates the Event "Detonate" we created using the Event Dispatcher. The actions that will occur due to this event will be created on the Level Blueprint.

After the compilation of the "Detonator" Blueprint, it is ready to be added to the Level. The image below shows the "Detonator" in a table.


We now need to place the Actions in the Level Blueprints that will generate the explosion. Select the "Detonator" Blueprint that was placed on the Level and then open the Level Blueprint. Right click on the Graph and select "Add Detonate":


"Detonate" is the event generated by the "Detonator" using the Event Dispatcher. Adapting the example from the previous article, the script of the Level Blueprint becomes:

Click to enlarge

Run the game and touch the "Detonator" to start the countdown. When it reaches zero the explosion is generated:


Using Event Dispatcher we separate the "Detonator" Blueprint of the explosion that is generated. This way we can use the "Detonator" in several levels generating different explosions without changing the "Detonator" Blueprint.


Next: Tick Event and Latent Actions in Blueprints

Prev: Using Level Blueprints
Table of Contents