Creating a Blueprint Interface¶
To create a Blueprint Interface, you'll need to select Add > Blueprint > Blueprint Interface from the Content Browser.
Definition¶
The Blueprint Interface Editor is basically a watered-down version of a Blueprint Class Editor. You can only declare functions and events in an interface, but not variables. The class interface and its member functions support all the same settings as a typical class, except that the functions cannot be pure. See Blueprint Classes for more info on the available settings.
As an example, let us define an interface for "Breakable" objects in the world that gives the player resources when destroyed. The interface will need two functions: Damage and Spawn Resources.
The Damage function will take an input of type float and return a boolean. The function will be called when the object is damaged, and it will return true if the object is destroyed.
On the other hand, Spawn Resources will only accept an input, an Enumeration indicating the player's Luck. The function has no return value.
Usage¶
To implement an interface in a Blueprint Class, e.g., Tree, we need to add it to Interfaces > Implemented Interfaces in the Class Settings.
The declared functions will now be available under the Interfaces panel:
The Damage function will be an overrideable function, since it has a return value, while Spawn Resources will appear as an event, since it lacks any.
Other classes can invoke these functions without casting or even knowing if the target object implements the interface. For example, in a Pickaxe class, we can invoke the functions on any actor it collides with:
If the target does not implement the interface, the node will not do anything, but the execution will continue on and the output pins will be initialized with default values.