Direct Blueprint Communication

Direct Blueprint Communication is a simple method of communication between Blueprints/Actors. It is used by creating an object reference variable that stores a reference to another Actor/Blueprint. Then, we can call Actions using this object reference variable as the Target input parameter of these Actions.

As an example, let's create a Blueprint called BP_LightSwitch. The initials of BP_ are a naming convention used to facilitate the identification of a Blueprint asset. BP_LightSwitch has an object reference variable of the Point Light type that references a Point Light placed in the Level. When the player overlaps the BP_LightSwitch Blueprint on the Level, it toggles the visibility of the Point Light.

Follow these steps:

  1. Create or use an existing project that contains starter content.
  2. Click the Add New button in the Content Browser and choose the Blueprint Class option.
  3. On the next screen, choose Actor as the parent class.
  4. Name the Blueprint BP_LightSwitch and double-click it to open the Blueprint Editor.
  5. Click the Add Component button in the Components panel and choose the Static Mesh Component. In the Details panel, choose the SM_CornerFrame Static Mesh, as shown in the following screenshot. This Static Mesh is a simple visual representation of our light switch. Also, change Collision Presets to OverlapAllDynamic so that Static Mesh will not block the player's movement:

  1. In the My Blueprint panel, create a new variable named Light. In the Details panel, click the Variable Type drop-down menu and search for Point Light. Hover over Point Light to display a submenu and then choose Object Reference. Check the Instance Editable attribute, as shown in the following screenshot:

  1. Drag the Light variable from the My Blueprint panel and drop it into the Event Graph. Choose the Get Light option to create a node. Drag from the blue pin of the Light node and drop in the graph to open Context Menu. At the top of Context Menu, there is a checkbox named Context Sensitive. If checked, the list of nodes is filtered to Actions that can be used in the current context, which, in this case, are the Actions that can be used with a Point Light object reference. Search for toggle and choose the Function named Toggle Visibility (PointLightComponent), as shown in the following screenshot:

  1. Right-click Event Graph and add Event ActorBeginOverlap. Drag from the blue pin of the Light node, drop it in the graph to open Context Menu, and add the Is Valid Macro, which is the one with the white question mark. This Macro is used to test whether the Light variable is referencing an instance. Connect the nodes, as shown in the following screenshot. Compile this Blueprint:

  1. In the Level Editor, look at the Modes panel on the left. Point Light is in the Basic category, as shown in the following screenshot. Drag Point Light and drop it somewhere in the Level to create an instance. In the Details panel, change the name of the Point Light instance to Lamp and set the Mobility attribute to Movable:

  1. Drag the BP_LightSwitch Blueprint class from Content Browser and drop it in the Level in a place near the Point Light instance that we added to the Level. The next screenshot shows the Details panel of BP_LightSwitchThe Light variable appears in the Details panel because we checked the Instance Editable attribute. Click the drop-down menu of the Light variable to show all Point Light instances that are in the Level and select the Point Light instance that we renamed to Lamp in the preceding step. Essentially, this is Direct Blueprint Communication. BP_LightSwitch has an object reference to another Actor/Blueprint and can call its Actions:

  1. Click the Play button to see the BP_LightSwitch Blueprint in Action. Every time your character overlaps the instance of BP_LightSwitch, it toggles the visibility of the selected Point Light. The following screenshot shows an example using the Third Person template. The Point Light variable is on the wall, and the BP_LightSwitch Blueprint is on the floor:

..................Content has been hidden....................

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