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, BlueprintImplementableEvent, Category="Trigger Reaction")
bool ReactToTrigger() const;
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 and pure virtual functions are supported. Although our sample function is trivial and could reasonably be written in the .h file, we'll write it in the .cpp file. Your .cpp file (e.g.
ReactToTriggerInterface.cpp) should now contain this:
bool IReactToTriggerInterface::ReactToTrigger() const
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.