Latest news

Unreal C++ Interface and what to do with it?

When using C#, Interfaces are something easy and quite natural to implement. But since we’re using Unreal, it has been quite a challenge to find reliable information for implementing certain design patterns in C++.

This is especially true for Interfaces, as the pure C++ implementation has nothing to do with the way it’s implemented in Unreal.

I’ve learned to love the interfaces in Unreal Engine, as they provide a nice way to keep track of elements in your game. In a way, Interfaces can almost be used like the Tags in Unity, and functions such as FindGameObjectsWithTag

Interfaces with pure C++

Here is how it’s implemented with pure C++ code:

class IInterface
{
    public:
    virtual void FirstFunction() = 0;
};

class BaseClass
{
    ...
};

class DerivedClass: public BaseClass, public IInterface
{
   ...
};

If you need to call the function created via the interface, it behave exactly the same way than a regular function, so you will need to do

DerivedClass* Class;
Class->FirstFunction();

As you can see, it’s quite similar to C#.

The UE4 way

As usual, the first thing you should do, it’s to google “UE4 Interface C++”, If you’re lucky you will find the documentation of Unreal. You can have a quick look at it, to see what you’re missing: Unreal Docs Interface

So According to Epic, the structure of the interface is a bit more complicated than regular C++.

First you need to declare a UINTERFACE. starting by a “U”, this is an empty class and is used by Unreal Engine’s reflection system


UINTERFACE(MinimalAPI, Blueprintable)
class UActionable : public UInterface
{
    GENERATED_BODY()
};

And then you need to declare another one, but this time starting by a “I”, this is the actual interface.

class PROJECT_API IActionable
{
    GENERATED_BODY()

    public:
    /** Add interface function declarations here */
}

the code should look like that:

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Actionable.generated.h"

UINTERFACE(MinimalAPI, Blueprintable)
class UActionable : public UInterface
{
    GENERATED_BODY()
};

class PROJECT_API IActionable
{
    GENERATED_BODY()
 
public:

    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Actionable")
    void OnActivate();
	
}

Once the interface created, you can add that to your classes, just like this:

FILE : ActivateActionable.h

class PROJECT_API AActivateActionable : public AActor, public IActionable
{
    GENERATED_BODY()
public:
    virtual void OnActivate_Implementation();
};

… And that’s where the documentation of Unreal engine kind of stop

What Now?

Now if you want to reference one of the class using an interface, you can simply declare it in the header file as follow:

UPROPERTY(EditAnywhere, Category = "Action")
TScriptInterface<IActionable> Actionable;

And to use it do as follow:

if (Actionable != nullptr)
{
    Actionable->Execute_OnActivate(Actionable.GetObject());
}

notice that the “Execute_OnActivate” needs to get the UObject of the interface.

But there is more, The amazing feature that Unreal Engine has with the interface, it’s that you can use it almost like a tag to mark and search your class.

Get All Actors with Interface

Brace yourself, it’s going to get strange with the function “GetAllActorsWithInterface“, the documentation on Unreal Engine is quite empty for it. The most interesting you may find is some blueprints.

In c++, the usage is as follow:

TArray<AActor*> ActionableActor;

UGameplayStatics::GetAllActorsWithInterface(GetWorld(), UActionable::StaticClass(), ActionableActor);

For some reason, you need to use the “UINTERFACE” and not the Interface itself, this function will output all the Actors having the interface. Once you get that, you need to cast it to the interface you have.

TArray<AActor*> ActionableActor;

UGameplayStatics::GetAllActorsWithInterface(GetWorld(), UActionable::StaticClass(), ActionableActor);

for (AActor* Actor : ActionableActor)
{
    IActionable* Interface = Cast<IActionable>(Actor);

    if (Interface != nullptr)
    {
        Interface->Execute_OnActivate(Actor);
    }
}

That’s it for now, hope you know a bit more about interfaces in Unreal and the way to use it 🙂

exiinUnreal C++ Interface and what to do with it?