How to call a C++ interface method from blueprint?

I have a class, MyClass, that has a member of type TScriptInterface<MyInterface>. I have it defined like this inside MyClass.h:

UPROPERTY(EditAnywhere, BlueprintReadOnly)
TScriptInterface<MyInterface> MyProperty;

Then, inside MyInterface.h, I have a method that I want to call using a reference to MyProperty in MyClass’ derived blueprint:

FORCEINLINE UFUNCTION(BlueprintCallable, BlueprintPure)
virtual bool CheckSomething() { return false; }

Of course, this interface is implemented by a C++ class, that overrides this virtual method and gives a proper implementation to it.

Now, when I get into MyClass’ blueprint, I can access the MyProperty member, but if I try to drag a line out of it, I cannot see CheckSomething.

Of course, I can cast the interface instance to a concrete class instance, but that would obviously undermine the reason why I’m using an interface.

How can I do this? Am I missing a function specifier in the declaration of CheckSomething(), or a property specifier in the declaration of MyProperty?

Thanks in advance.

I think your FORCEINLINE macro is in the wrong place and preventing UHT from parsing the UFUNCTION.

You should be getting the following errors :

  • BlueprintPure specifier is not allowed for interface functions
  • Blueprint implementable interfaces cannot contain BlueprintCallable functions that are not BlueprintImplementableEvents

Try this instead :

UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
bool CheckSomething();
virtual bool CheckSomething_Implementation() { return false; }

You can also skip the second error by preventing blueprint interface implementation, although that could be shooting yourself in the foot for the future, but who knows

UINTERFACE(MinimalAPI, Meta=(CannotImplementInterfaceInBlueprint))
class UMyInterface ...

class IMyInterface ...

    UFUNCTION(BlueprintCallable)
    virtual bool CheckSomething() { return false; }
1 Like

Thank you. this worked like a charm. I ended up going with your second suggestion, since I would like to keep the code as simple and readable as possible, but I’ll switch to the first approach as soon as I need it.

I have some follow-up questions, if you don’t mind:

  • Is the place where I’m specifying FORCEINLINE wrong, or should I simply not use FORCEINLINE at all in interfaces?
  • If I cannot use BlueprintPure, that means I need to plug something to the execution pin of the node. How could I do so if I want to use it inside the transition rule of a state machine?

And, as a side note, I think that the Meta=(CannotImplementInterfaceInBlueprint) specifier is equivalent to NotBlueprintable (UE 5.1.3 UHT suggested it to me, and it seems to work as well). Feel free to correct me if I’m wrong.

I guess you can use it like this: virtual FORCEINLINE bool CheckSomething()

But chances are it’s not gonna be super useful, because :

Generally for objects in UE4 we only use pointers so the virtual needs to be resolved (no inlining).
There might be a use case for Super calls though I’m not sure about that.


If you really need a pure call in this scenario I guess you could always add a pure static wrapper for some added clunkiness :stuck_out_tongue:

UFUNCTION(BlueprintPure)
static bool K2_CheckSomething(UObject* Target)
{
    if (Target->Implements<UMyInterface>())
        return IMyInterface::Execute_CheckSomething(Target);
    return false;
}
1 Like

The inline part makes all the sense, I don’t know why I didn’t think about that before. Removing FORCEINLINE it is then.

About the second part, is quite a bummer that interfaces have that lack of functionality. I know it’s a very specific scenario, but this kind of holes in interfaces functionality can be quite a pain in the long run. I will consider what options I have. Anyway, thank you for your help and your detailed answers, I really appreciate it.