How it works...

UInterfaces are implemented as pairs of classes that are declared in the interface's header.

As always, because we are leveraging Unreal's reflection system, we need to include our generated header file. Refer to the Handling events implemented via virtual functions recipe in Chapter 5, Handling Events and Delegates, for more information.

As with classes that inherit from UObject, which uses UCLASS, we need to use the UINTERFACE macro to declare our new UInterface. Passing in the class specifier of MinimalAPI causes only the class's type information to be exported for use by other modules. 

For more information on this and other class specifiers, check out: https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Reference/Classes/Specifiers.

The class is tagged as UE4COOKBOOK_API to help with exporting library symbols.

The base class for the UObject portion of the interface is UInterface.

Just like UCLASS types, we require a macro to be placed inside the body of our class so that the auto-generated code is inserted into it. That macro is GENERATED_BODY() for UInterfaces. The macro must be placed at the very start of the class body.

The second class is also tagged as UE4COOKBOOK_API, and is named in a specific way.

Note that the UInterface derived class and the standard class have the same name but a different prefix. The UInterface derived class has the prefix U, and the standard class has the prefix I. This is important as this is how the Unreal Header Tool expects classes to be named for the code it generates to work properly.

The plain native Interface class requires its own autogenerated content, which we include using the GENERATED_BODY() macro.

We declare functions that classes inheriting the interface should implement inside IInterface.

Within the implementation file, we implement the constructor for our UInterface, as it is declared by the Unreal Header Tool and requires an implementation.

We also create a default implementation for our GetTestName() function. Without this, the linking phase in the compilation will fail. This default implementation uses the unimplemented() macro, which will issue a debug assert when the line of code is executed.

For more information on creating interfaces, check out the following link: https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Reference/Interfaces.