ostanton::blog::posts::ue_bp_interfaces

using namespace ostanton;

Unreal Engine Blueprint Interfaces

03 Oct 2023

What are they?

Interfaces in Unreal are a way for classes to interact with each other without needing hard references to user-defined classes. I will only talk about Blueprint Interfaces in this post, because I don't know C++ (enough) to use it with Unreal.

Either way, what I mean by no references, is you only save variables that would normally be your own class, like BP_FirstPersonCharacter for example, as just an Object or an Actor. You then implement an Interface in BP_FirstPersonCharacter, and call functions to that Interface from elsewhere.

Real World Example:

An easy example of how useful Interfaces are is with the HUD class. Not widgets, but the HUD class itself. You generally don't want to have references to specific widgets anywhere in your Blueprints, they should only belong in the HUD class. So if we make widgets in the HUD class, how do we access them in other Blueprints? Easy, you don't. You don't need to access them. You can create an Interface, call it BPI_HUD for example, implement it in the HUD class, and add functions like UpdateHealth or something, which then gets W_HUD (the HUD widget) and calls a function on that to update a health bar. This way, you only parse in the health value into the Interface function, and the HUD widget doesn't get used outside of the HUD class, keeping it clean and contained.

So, you've seen how useful Interfaces are, with HUDs, Player Controllers, Player Character, literally anything. Any and all classes are able to implement Interfaces. So, how do you create and use them then?

Creation:

Right click in the Content Browser, and go to Blueprints->Interface. Name it something. The general prefix for Blueprint Interfaces is BPI_, but it really doesn't matter, as long as it's clear and coherent with what it is.

Create the Interface

Open it, and a function is pre-made for you.

The initial interface

These are read-only, and act almost like a mix between Custom Events and functions. If you create a function without any outputs, then it acts like an event. It might be confusing starting off, but when you get the hand of them they're great. Don't worry about the node graph in the Interface, it's not really needed. The Interface itself doesn't do any logic, it's the Blueprints that "implement" it that do stuff. Think of it like creating a Custom Event or function in whatever Blueprint, let's say HUD, and casting to HUD and calling the event. The difference here is that the Interface makes that event separate from the HUD, so it can be used with whatever Object, and called by any Object. It's simply just a different way of defining your Custom Events and functions, and having them be Object-agnostic.

Functions:

Now, you can name the function something, let's go with UpdateHealth. It will be used to call a function in our HUD widget to set the health bar amount. It will have an input, the health value to set the bar to.

UpdateHealth

Implementing the Interface is very easy. Simply go to the Class Settings of your Blueprint you want to implement the Interface in (in our case it's the HUD class), and near the bottom there is a section for Interfaces. Add it.

Implement the Interface

Once implemented, you'll find that there is a new tab bar with the Blueprint's functions and variables, aptly named "Interfaces". You can double click on an Interface function to implement it. There, you can create the logic for what should happen whenever another class calls this function. This is not a tutorial on widgets and the like, so I will not explain how I made the SetHealth function in the Health widget. OK fine I will. Health is just its own widget with functions that's in the HUD widget. Bang, easy.

Define UpdateHealth

Now it's implemented and logic runs on it, we can call it somewhere. For example in our character Blueprint when we set its health. Notice we aren't casting to our own HUD class, or need a reference to it at all. We can just use Unreal's built in one. This is more efficient, as it means we don't need to load our own class which is probably a lot heavier than the default one with the widget references it has (yes, casting to it will also then load the widget references, etc. that we don't need and won't use, hence the Interface function to mitigate that).

Call UpdateHealth in player

And that's pretty much it for Interfaces. Functions with return values are the same but just look like functions instead of events, and are useful for getters (even though they can't be pure sadly). If for whatever reason you need to get a widget reference, if you can then return a generic widget object and use Interface functions in that widget instead of getting the user-defined UserWidget. It is a bit more work but it's so much more optimised and streamlined.

One final thing. You can categorise Interface functions, and I recommend categorising them. Having one overarching category for each Interface and sub categories for different things helps a lot, as the overarching category shows up as a main category in the add node menu. You can set sub categories with the pipe (|) symbol.

Set category

Using the category

::blog::tags