How do i expose a C++ Interface to Blueprints with a BlueprintCallable function?

Hello i have an interface that is implemented by some of my c++ classes, and each one overrides a function that returns it’s own value for each class. The problem is that i want to expose the interface to blueprints (by using UINTERFACE(Blueprintable)) and i just want to call that function in blueprints, but the compiler doesn’t allow me to have BlueprintCallable modifiers in Blueprintable interfaces, only BlueprintImplementableEvent is allowed (which i don’t want). What else can i do to make this work? Thank you.

I’ve faced this issue before. What works is to use both BlueprintNativeEvent and BlueprintCallable together while declaring your interface function. This optionally allows you to override it in BP as well, but in your case you don’t need to as you’re just interested in calling the function. Like this:

UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "SomeCategory")
void SomeInterfaceFunction();

Be sure to use this specific format to declare the function in whichever C++ class is implementing it:

// format:
//virtual <RetType> <YourFunctionName>_Implementation() override;

// from the example above:
virtual void SomeInterfaceFunction_Implementation() override;

This is necessary because the generated code behind the scenes is declared in this manner. Implement this function in your CPP with the this name too (i.e. _Implementation needs to be appended to the function name).

This should work fine - I just setup an interface like this now for my own work and I’m able to call it from Blueprints.

PS: Some additional information that may help down the line - if you do decide to implement your interface function in a Blueprint you might need to use the syntax Execute_SomeInterfaceFunction(UObject YourObject)* to call those interface functions from C++.

For a more detailed explanation about why all this is necessary check out this writeup and this wiki


[UPDATE - 12th Jan 2016]

I just stumbled upon some of my old project code that does exactly what you ask for even without needing BlueprintNativeEvent. Looks like marking the interface as CannotImplementInterfaceInBlueprint is key.

Here’s an example (dummy names):

UINTERFACE(meta = (CannotImplementInterfaceInBlueprint))
class UMyInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class IMyInterface
{
	GENERATED_IINTERFACE_BODY()

	UFUNCTION(BlueprintCallable, Category = SomeCategory)
	virtual void SomeInterfaceFunction();
};

Thanks, CannotImplementInterfaceInBlueprint works

When using “CannotImplementInterfaceInBlueprint”, how did you override the interface functions in your c++ classes? I keep running into compilation error LNK2001, now matter how I declare the functions in my c++ class. Thanks.

Create SomeInterface.h and empty SomeInterface.cpp (only include “SomeInterface.h”):

#include "UObject/Interface.h"
#include "SomeInterface.generated.h"

 UINTERFACE(Blueprintable, MinimalAPI, meta = (CannotImplementInterfaceInBlueprint))
    class USomeInterface: public UInterface
    {
    	GENERATED_BODY()
    };
    
    
    class ISomeInterface
    {
    	GENERATED_BODY()
    
    public:
    
        UFUNCTION(BlueprintCallable)
        virtual void SomeAction() = 0;
    };

Than create implementation:

UCLASS()
class ASomeImplementation : public ISomeInterface
{
    GENERATED_BODY()

public:

    void SomeAction() override;
};

Note: This work in UE 4.21.1

Thanks! Turns out I needed to make the function declaration in the interface pure virtual by adding “= 0” to the end of the function. I need to brush up on my C++ knowledge. Thanks again for the help :slight_smile:

thank you!