Declaring an interface class is similar to declaring a normal Unreal class, but with two main differences. First, an interface class uses the UINTERFACE macro instead of the UCLASS macro, and inherits from
UInterface instead of
UINTERFACE([specifier, specifier, ...], [meta(key=value, key=value, ...)])
class UClassName : public UInterface
Second, the UINTERFACE class is not the actual interface; it is an empty class that exists only for visibility to Unreal Engine's reflection system. The actual interface that will be inherited by other classes must have the same class name, but with the initial "U" changed to an "I".
In your .h file (e.g.
class UReactToTriggerInterface : public UInterface
/** React to a trigger volume activating this object. Return true if the reaction succeeds. */
UFUNCTION(BlueprintCallable, Category="Trigger Reaction")
virtual bool ReactToTrigger();
Functions can be written directly in the .h file, and this is commonly done when a function does nothing by default, or has a trivial behavior like returning false, zero, an empty string (or something similar). More complex functions can be written in the .cpp file to reduce compile times. This system supports pure virtual functions. Although our sample function is trivial and could reasonably go in the .h file, we'll write it in the .cpp file, which should now contain this:
// Override this function with any C++ code your project requires. Return true on success.
// Depending on your coding style or how you want to interpret the return value, you may wish to default to true.
The "U-prefixed" class needs no constructor or any other functions, while the "I-prefixed" class will contain all interface functions and is the one that will actually be inherited by your other classes.
Blueprintable specifier is required if you want to allow Blueprints to implement this interface.