Using Interfaces

In this How To, we will set up a Blueprint Interface which will execute different scripts in multiple Blueprints when called.

For this guide we are using the Blueprint First Person template with Starter Content enabled.

  1. In the Content/StarterContent/Props folder, Right-click on the SM_GlassWindow and choose Create Destructible Mesh.

    Interface1.png

    We are going to turn this Static Mesh into a Destructible Mesh which we will break when shot.

  2. Open the Destructible Mesh, then click Fracture Mesh and Save then close the Destructible Mesh Editor window.

    Interface2.png

  3. Right-click on the SM_GlassWindow_DM asset, and under Asset Actions, select Create Blueprint Using This....

    Interface3.png

    This will prompt you to enter a name and save location for the Blueprint (we used SM_GlassWindow_DM).

  4. Right-click inside the Content Browser, then under Blueprints select Blueprint Interface.

    Interface4.png

    This will again ask you to enter a name and save location for the Blueprint Interface (we used DM_Interface).

  5. Inside the Blueprint Interface, name the existing Function IsDestructible? and add the Inputs shown below by clicking New.

    Interface5.png

    Above we have created three Inputs which we can use to pass variables between Blueprints that implement this Blueprint Interface. We have create two Float variables, one called Damage which will be used to pass an amount of damage to cause to the object we hit and the other Strength the amount of Impulse Strength to apply. Lastly, we added a Vector called HitLoc to hold the location in which the object was hit so that we may pass that information through as well.

  6. Compile and Save then close the Blueprint Interface.

  7. In the Content Browser open the SM_GlassWindow_DM Blueprint, then click the Class Settings option from the tool bar.

    Interface6.png

  8. In the Details panel, click the Add button under Interfaces and add the DM_Interface.

    Interface7.png

    This will implement the Blueprint Interface in this Blueprint, allowing us to access the functions within it.

  9. Right-click in the graph and search for a ? and select the Event Is Destructible? Event.

    Interface8.png

  10. Hold Control and drag in the Destructible component from the MyBlueprint window and create the script below.

    Interface9.png

    Here, off the Destructible component we used Apply Damage and connected the damage, strength, location and direction requirements needed to apply damage to our glass window. The actual damage, strength and hit location information will be passed through the Blueprint Interface to this Blueprint from another Blueprint which we will do in a moment.

  11. Compile and Save then close the Blueprint.

  12. In the Content Browser under Content/StarterContent/Blueprints, open the Blueprint_CeilingLight Blueprint.

    Interface10.png

    Using the Blueprint Interface, we will do something different when we shoot this light.

  13. Add the DM_Interface through Class Settings then drag in the PointLight1 component and create the script below.

    Interface11.png

    Here, when Event Is Destructible? is called, we will first set the light to a green color. The second time it is called, we will set the intensity to 0 (turning off the light). Notice that we did not use the Damage, Strength or HitLoc information contained in the interface as it was not relevant to this Blueprint. By using a Blueprint Interface we can make a general function that is called in all Blueprints that implement that interface and function, and then determine what script is executed on each individual Blueprint.

  14. Compile and Save then close the Blueprint.

  15. Inside the Content Browser under Content/FirstPersonBP/Blueprints, open the FirstPersonProjectile Blueprint.

    Interface12.png

    This Blueprint includes the script for the projectile that is spawned in-game when the player fires their weapon.

  16. Add the DM_Interface through Class Settings then Right-click in the graph and add the IsDestructible?(Message) node.

    Interface13.png

    This will execute the function IsDestructible? and all Blueprints that implement the interface and function will also execute.

    If you do not see the node, un-check the Context Sensitive option in the upper-right corner of the context menu.

  17. Off the False pin of the Branch connect the IsDestructible? node and connect the Other and HitLocation pins.

    Click for full image.

    Above, we have added to the existing script that checks if the object that was hit is simulating physics. If it is not simulating physics, we call our IsDestructible? function in our Blueprint Interface and apply 1000 damage and 100 strength to the target (which is gathered from the Event Hit node's Other pin where we also get the Hit Location and assign it to our HitLoc variable to pass through).

  18. Compile and Save, then close the Blueprint.

  19. Drag the SM_GlassWindow_DM and Blueprint_CeilingLight Blueprints into the level then click Play to play in the editor.

    Shooting the glass will cause it to fracture and break apart.

    Example1.png

    Shooting the light once, will cause it to change to the color you specified in step 13.

    Example2.png

    Shooting the light a second time will cause it to turn off (also specified in step 13).

    Example3.png

This is one example of using a Blueprint Interface to pass variables between Blueprints and call functions which execute different scripts in the Blueprints that implement the interface. For more information on working with Blueprint Interfaces and other forms of Blueprint communication, refer to the Related Topics section below.

Tags