How to make an Interface Function which is overridable from both C++ and Blueprint?

Hello everyone,

I have created an interface including an overridable function (virtual function). I can override this function on C++ easily however how can I extend this function to the Blueprint? I tired to use the ‘BlueprintImplementable’ specifier but the engine wanted me not to use the ‘virtual’ syntax. In this case, I cannot override on C++. Is there a way to override a function from the both?

I tried both but no luck. They don’t accept virtual functions

Have you tried using BlueprintNativeEvent instead of BlueprintImplementableEvent?

BlueprintNativeEvent is overridable. Thing is, you don’t override the function it self FunctionName that you marked BlueprintNativeEvent but you override FunctionName_Implementation which is generated as virtual

BlueprintImplementableEvent makes event implementable only on blueprint

Simple mistake, my bad. BlueprintNativeEvent works. Thank you.

Hi @Core21 I do this all the time, it is an essential part of my development process. Once you understand how UE4 interfaces work it is quite easy to leverage, but very different from what you may be familiar with. First you can actually use the blueprint wizard in the editor to initially create your interface, I suggest doing it. It should look something like this:

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * Interface for my Game
 */
class MYGAME_API IMyInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

    /** 
* Get some important component thinggy
* @return UActorComponent
*/
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="Interfaces|MyInterface")
class UActorComponent * GetSomeComponent() const;
};

Now that you have defined your interface you can now implement it in Both C++ and Blueprint. To do this in C++ simply make a declaration in your class as so

 /** 
* Get that Stats Component for the Character
* @return UActorComponent
*/
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="Interfaces|MyInterface")
class UActorComponent * GetSomeComponent() const;

 // Implementation of AMyClass::GetSomeComponent
 virtual class UActorComponent * GetSomeComponent_Implementation() const override;

And there you go, you should be able to implement the interface in Blueprint as well with out any issue. In child C++ classes that you want to override the interface function, it is the “_Implementation” function that you will override in stead.

Oh I almost forgot calling interfaces in C++ is a bit different than your usual C++ way. You first must check if the the object you are using implements the interface, this is done with a built in function Implements() I actually made a macro for this that I define in all my project UE4 headers:

// INTERFACES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/** Used to implement Interface Body and ImplementsInterface function in one easy step */
#ifndef IMPLEMENT_INTERFACE
#define IMPLEMENT_INTERFACE(className) public: static bool ImplementsInterface(const UObject * object) { return IsValid(object) && object->Implements<className>(); }
#endif

Essentually it is that “myObject->Implements();” part you need to call. Having this setup as a macro in your project header file makes life just a little bit easier when it comes to creating many C++ Interfaces that also implement in Blueprints.

You then define it within the body of your IInterface object, usually at the bottom of your interface functions:

IMPLEMENT_INTERFACE(UMyInterface);

Notice that it leverages the UInterface part and not the IInterface part. Now when-EVER I need to use an interface in C++ I do the following:

if(object != nullptr && IMyInterface::ImplementsInterface(object))
{
    UActorComponent * myComp = IMyInterface::Execute_GetSomeComponent(object)
}

UE4 interfaces in C++ actually create static functions that you use to call them, it is sort of a reflection based thing. But instead of casting to an interface pointer and then calling the function (which never worked for me before version 4.18, still not sure if it works now but that was when I was figuring all this out), you instead call the “Execute_” static member of the generated interface passing in the object you want to call the function on. Again this is sort of how reflection works in other languages, UE4 created their own auto generated reflection system for C++ classes that is very similar to C# and other languages that can leverage reflection.

2 Likes

Thank you for the detailed explanation. The way you implement interfaces is better than mine.