Using Interfaces in UE4

A short guide on using Interfaces.

6 min read
1 views
Intermediate

Interfaces are an amazing thing that's in programming. An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class. Meaning any class that extends an interface has to implement the methods of that interface. You can also add more than 1 interface to a class and an abstract class you can't. More on this later. The simplest way to explain what an interface is would be it's a class with no implementation.

 

Why?

Say you have an interaction system and you want to only interact with specific actors in your level. You can achieve this in a ton of different ways, but generally, they're messy and a nightmare to maintain (with the exception of having an abstract class such as BaseItem and then Apple is derived from BaseItem). With interfaces, you can set specific information (not variables, but methods such as Interact or Consume or even Drop) on a class (which would force Apple to implement Interact, Consume, and Drop and the same with Banana, etc) or even you can use an interface to be an identifier (for example when using object->GetClass()->ImplementsInterface(MYINTERFACE)).

 

When?

So if you're wanting 2 actors to have the same properties such as Health and a Name then you should probably use an abstract class. If you're wanting to interact with both of those actors then create an interface to handle that. You can use an abstract class (or rather a base class - it holds all of the common variables for your child classes) and then have the interface for interaction.

If you just want to interact with an actor and have no data for that actor then just create an interface so you can interact with it.

 

How?

Ok, cool so now that you understand. Let's get to it. Because I mainly write C++ over BP I'm going to start this guide with C++. If you just want to download the project and learn on your own then feel free to here.

This is the C++ way. If you're wanting to just do interfaces in BP then scroll down more. :)

  1. Right-click in your Content browser -> New C++ Class or File -> New C++ Class (scroll to the bottom and choose Unreal Interface) Click Next.
  2. Type in your Interface name and path then click Create Class
  3. The same way above, create an actor class and name it whatever. I've named mine InteractableActor.
  4. Go to Edit -> Project Settings then Engine -> Input, now add a new Action Mapping.
  5. Cool, now you're done. Next guide! Just kidding, now you should open Visual Studio or whatever IDE you're using if you haven't already.
  6. Go into your Character class and add the following code.
  7. Add the following to the SetupPlayerInputComponent.

  8. Head over to your Interface class and make it look like mine.

    Basically, you just want to add your methods to the second class in the file.

  9. Now in your InteractableActor class add the following. At the top where it says public Actor you need to change it to public Actor, public IInteractInterface. This implements the interface InteractInterface so you can use its methods. You're also going to have to include the interface's header file at the top of your class.

    Cool, now you're pretty much done. Go into the editor and create a BP derived of InteractableActor and place it into the world. (make sure you add a mesh to the actor so you can see it) If you want to override the C++ implementation in BP you certainly can. Just right click and Implement Function and it'll create the node for you.

 

This is the BP way. If you're wanting to just do interfaces in C++ then scroll up, you passed it. :)

  1. Right-click in your Content browser -> Blueprints -> Blueprint Interface. I named mine BP_Interface_Interact.
  2. Open your new BP interface and rename the function to Drop.
  3. Create a new Actor BP and click Class Settings.
  4. Add the interface that you just created. 

  5. Right-click and Implement Function.

  6. Go into your Character class and add the following.
  7. The last thing to do is to add the actor to the level.

 

until next time