skip to Main Content

Creating Interface classes in UE4 using C++

Using interfaces can make your code more flexible and save you some time from writing the same code again and again. Imagine that you are creating a game which has say 500 actors that are totally different from each other and you want some specific functionality in all of them. Moreover, let’s say that in the 350 of 500 actors the functionality is going to be the same, and the other 150 will have their own functionality. In this case, using an interface will save you so much time.

Interface explanation

You can think of an interface as a contract. Each class that follows this contract is essentially bound by some rules (of the contract). For example, if your interface contains a DoSomething() function, all the classes that are following the specific contract, are also going to have the same function, otherwise you code will not compile.

Adding an Interface

Currently there are two ways to add an interface inside your project. One method is to follow to official UE4 documentation.

For another method, keep on reading! The main difference between my method and the one that is written in the official UE4 documentation is that my interface cannot be exposed inside the Editor.

From your editor create a new C++ class which inherits no class (choose the None option).

interface_addclass

I’ve named my class IMyInterface. The initial “I” character is a naming convention for all interface classes.

When the visual studio loads up, delete the default constructor and destructor of your class and declare the following function:

virtual void DoSomething() = 0;

The “=0” right after the declaration of the function means that this function contains no logic and is intented to be overriden in all the classes that inherit our interface.

When you’re done with that, create a new C++ class which inherits from the Actor class.

When visual studio loads up, add the necessary header file before the .generated.h library:

#include "IMyInterface.h"

In order to inherit our interface place a comma near the public AActor class and add it’s class name like the following line of code suggests:

class INTERFACEPOST_API AMyActor : public AActor, public IMyInterface

Then, override the the DoSomething function like so:

virtual void DoSomething() override;

The next step is to go to your Actor’s source file and implement the following logic of the DoSomething function:

Then, you can call that function from inside the BeginPlay function.

As a last step, place your C++ class somewhere in your level. When you click play the Doing something message will appear.

Avatar photo

This Post Has 2 Comments

    1. Since this interface does not derive from the UObject class I don’t that this is possible.

      When it comes to the casting your interface class I think that dynamic cast should do the trick.

      -Orfeas

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top