Let’s talk about interfaces in game development

How to implement a simple interface for interactive objects on a map with Unreal Engine 4, C++ and Blueprints,

For this example, I am using Unreal Engine 4.26 with C++ and Blueprints.
You can download the project files here


You can follow along with this tutorial on the Video tutorial version here:

What is an interface?

An interface has a straightforward syntax that looks very much like a class definition.
It helps you to declare the high-level functionality for a particular feature, for example, interactables!
An interface only allows you to define functionality, not implement it, this means you declare the signature of the function but not the body of that function.

An interface is a contract between a class and the interface.
This means the compiler will ask you to implement each function’s body defined on the interface.
A class can extend from multiples interfaces.

Why using interfaces?

Sometimes this is more of a personal choice, but I think they keep the code clean.
This example makes sense to use an interactable interface because I will provide the interactive functionality to a Cat Character and 2 Actors on the map.
The 3 elements are different types, and this is the main reason an interface makes sense, as I couldn’t extend the 3 of them from an abstract class.

The other reason is, the Player Character doesn’t need to know what type of object implements the interface.
It only needs to know that there is an object that is close and needs to interact with it.

The example

Let’s jump to the practical example in UE4. We have 3 interactable objects on the map, a Cat (Extends from Character) a Sashimi item that you can pick up (an actor) and a crate (an actor) that you can push in a direction.
You can interact by pressing the E key on the keyboard with the 3 of them when you are close :

If you interact with each element:

  • The cat asks for food when you interact with them
  • The box that moves when you interact with it
  • The item sashimi can be picked up pick up when you interact with it.

Remember, you can download this example from here:


Creating the interface in C++

Our Interactable interface is going to contain 3 basic functions:

  • GetName: A name of the interactive to show on the UI
  • GetQuestID: A quest ID just in case the interactive can give us a quest. For example, the cat will ask you for food when you interact with them.
  • OnInteract Event: An event with implementation in C++ and in Blueprint. This is very handy, we can just have code in C++, or we can extend the functionality on BP, for example, playing a sound.

To create the C++ interface, we right-click on the content browser and select New C++ class.

On the browser, find the Parent Class: Unreal Interface.

In the next step, we give the name Interactable and let it compile.

When the compiler ends, you will have an empty class, and you can start adding the functions:

As you can see the function GetName and GetQuestID return an FName. This is the equivalent of creating a variable and declaring a getter for the variable.

Both of these functions are pure virtual functions denoted by = 0 and the keyword virtual.

As you can see, the interface defines a high-level definition for any interactable object in the game. This means that any actor or class that implements this interface will have to implement both functions and the OnInteract event.

Let’s first check the Sashimi item that you can pick up.

The pickup is an actor that also extends from a basic class which contains a handy component, a Box Trigger.

I use this Basic Interactive class whenever I need an actor with a trigger to detect if the player is close.
You can skip this step and just copy and paste the functionality on each actor class you create.

Basic implementation of a trigger
Begin and End Overlap methods for the Trigger

The ItemInteractive, extends from BasicInteractive to inheritance the BoxComponent trigger, and implements the interface IInteractable:

As you can see, we have a UPROPERTY ItemClass, which is the item that the player can pick up.
We also have a UPROPERTY ItemName to show on the UI

As required when working with interfaces, we have to implement every function is declared on the interface IInteractable

On the cpp file, we implement the body of the interact event and the GetName, and GetQuestID

If we test this code, we can get close to the sashimi, a UI will be shown with the name of the item, and when the player interacts, the sashimi appears on the player’s hands.


Now, how do I connect the player, the interface and 3 objects?

I use the player character for that!

Every time the player is close to an object that implements the Interactive interface (the cat, the box or the item); the function OnEnterActor is called.
This function receives a reference to the object (the actor wich implements the interface)

When the player pressed the E key, it will call the event OnInteract if the reference exists.
In the previous example, the item extends from BasicInteractive and the interface. When the player enters the trigger, the player will receive the sashimi actor.
When the player moves away from the item, the reference is removed (or set to null) calling the function OnLeaveActor.

On the player character, I keep the reference to the actor only if the actor implements the interface:

HowToCharacter, interface and actor references

As you can see, I check if the InteractiveActor is not null, if it is not, I use UKismetSystemLibrary::DoesImplementInterface to check if that actor implements the interface.

If that is true, I will reference the actor, and with a cast, I obtain the interface.

The function OnLeaveActor simply set both references to null:

In this simple way, I can detect if we are close to an interactable actor.

Finally, when the player presses the E key if the interactive exits, it will call the interface’s Interact event.

The actor implementing the interface will do whatever action is programmed to do.

The example of the item, the sashimi is removed from the map and spawned on the player’s hand.

That line of code Execute_OnInteract is the trickiest one. This is the only way to call a blueprint event using interfaces in C++.

It also requires the use of the specific actor, so I need the reference to the actor that implements the interface.
(Bit tricky and weird!)

I use the Player Controller to hock up the input. I keep a reference to the character on the Player Controller, and every time the player press the action Interacts, a function Action is being called:

And here comes what makes interface especial:

We can use this interface with any type of actor to provide interactivity, and the player doesn’t need to know the specific actor.

The other example I’ve got is a box that extends from BasicInteractive. When the player pressed the E key, it will move in some direction.

Finally my favourite example, the cat!

The cat is another character with a BoxComponent trigger on its own and implements the interface Interactable.

I use the trigger in a similar way as in the item. When the player is close, I will send a reference to the cat character.

When the player interacts with the cat, a UI shows an interface with information about a specific task the player has to do, bring food to the cat to get a secret.

What you can see in this snippet code, is simply a search of a quest on the game mode (this is for a separate tutorial).
I have an asset table with quest information, if you are curious, you are more than welcome to check the code. I will reserver that for another tutorial!
As you can see, I can reuse the same generic logic to represent 3 various interactions!

I really like how tidy the code looks with interfaces, and they are a powerful tool to keep in mind!
Characters and Actors in UE4 are not exactly the same, with this interface, I can reuse the same logic.

I hope you enjoyed this tutorial!

I genuinely enjoy writing about programming, maths, videogames and combine everything together!

I am currently working on a new indie game called Witchery Academy.
I am making this game with Unreal Engine 4, and it’s great to share some of my tricks and systems I use.

In Witchery Academy., you live the adventures of a wizard apprentice, live at the academy and attend different classes like the botanic class where you take care of your own garden.
And you have a spirit companion which is a a cat!

If you like cats, wizards, magic and a soothing experience, this game is for you!

If you don’t want to miss any updates, you can sign up for our newsletter:

I am Bee, an in indie developer creating Witchery Academ a life sim where you live the adventures of a wizard apprentice with your spirit companion

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store