How to create an interface in C++

I looked at this turotial on the wiki, but I’m struggeling to figure out where he created the header and source file and where the “*.generated.h” file came from… I just get those when I create a class from within the editor, but I can’t find UInterface as base class there.
I already tried to do code the interface from scratch (by selecting “none” as base class in the editor) and simply adding a similar code to my newly generated class… But this gives me a lot of errors when compiling from within the editor and also from within Visual Studio. When I remove the “Movable.generated.h” include (which wasn’t generated) two other errors show up…

Thanks for your help!

Hello, Benjamin

To create an interface in C++, you need to do the following:

  1. In AddCode window in Unreal Editor, check “All Classes”, select class Object and create a class, derived from it (named “UsableInterface” in the example).

  2. Change the .h file of your newly created class to the following:

    #pragma once

    #include “Object.h”
    #include “MyInterface.generated.h”

    UINTERFACE(MinimalAPI)
    class UUsableInterface : public UInterface
    {
    GENERATED_UINTERFACE_BODY()
    };

    class IUsableInterface
    {
    GENERATED_IINTERFACE_BODY()
    };

  3. Change your .cpp file to the following:

    #include “InterfacesQuestion.h”
    #include “UsableInterface.h”

    UUsableInterface::UUsableInterface(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
    {
    }

Please note, that UInterface is only meant to be a wrapper for the IInterface. Thus, inheritance should be done with IInterface.

Hope this helped!

Check this out: Interfaces | Unreal Engine Documentation