Tip for exposing C++ Interfaces in Blueprint

If anyone is trying to call the C++ Interface from the Widget’s Blueprint directly, and having trouble compiling correctly, there are at least two ways to go about it.

The first way is to follow the unreal documentation directly, and declare the methods with BlueprintNativeEvent. But then to override them in C++ , you have to declare them with a _Implementation suffix. This allows it to be implemented in either BP or C++, but is also a bit ugly.

Luckily, I found this post the unreal forums where all you need to do is add this meta tag to the UINTERFACE macro: meta = (CannotImplementInterfaceInBlueprint). Then declaring the method UFUNCTION(BlueprintCallable) will expose it and it will call the subclassed implementation!

Edit: It seems I was missing BlueprintType from the UINTERFACE attributes. Which is needed to actually expose the interface to Blueprint.

So my interface ended up like this:

// This class does not need to be modified.
UINTERFACE(BlueprintType, meta = (CannotImplementInterfaceInBlueprint))
class UMenuInterface : public UInterface
{
	GENERATED_BODY()
};

class MULTIPLAYER01_API IMenuInterface
{
	GENERATED_BODY()

private:
	UFUNCTION(BlueprintCallable)
	virtual void Host() = 0;
};

This may work but it potentially breaks a key rule for c++ which is one class one header. It also adds in an extra layer of complexity by adding in another layer of inheritance which makes it more difficult to maintain.

It may seem simpler on the surface but could make things more colex later and like I said, from a maintenance point of view it is definitely less manageable.

It is fine for these projects as they are small but as you create larger projects, you may find more and more inheritance, adding layers to that inheritance will cause more issues than they solve.

You generally want to keep inheritance to 1 or 2 levels.

Thanks for the heads up!

In my case, PuzzlePlatformGameInstance still just inherits from UGameInstance and IMenuInterface, like in the lecture. The only difference is I’m getting the GameInstance in the Widget Blueprint and casting it to MenuInterface, then calling Host() from there.

So I don’t have a MenuWidget c++ class (which is ironic since I vastly prefer c++). But my reasoning was based on this post from lecture 8.

But also because I’m starting to get confident with Unreal and just wanted to challenge myself to do it on my own :slightly_smiling_face:.

Yeah, now that I’m adding interface methods, I’m seeing that using the _Interface suffix is actually beneficial, since it indicates exactly what inheritance these methods are overriding.

Interfaces are powerful. Something I learned from C# development where interfaces cannot be more than declarations. The key to interfaces is keeping this simplicity. As soon as they get complicated they lose that power they offer over time as they get more complicated.

Privacy & Terms