Unable to implement a C++ interface in Blueprint?

Hi!

Loving the C++ reflection facilities of UClass/UObject. Very cool that an object can provide implementation either through native code or through Blueprints.

I have however what looks to be a restriction in code, one that prevents these features working out to their beautiful conclusion.

I have declared an interface with a function in C++. I can specify in the Blueprint editor that my blueprint should implement the interface. I can see the function, and can provide a blueprint implementation.

All good so far. The problem comes when I try to use the interface in C++. It is my understanding that InterfaceCast<> is the correct way to resolve an interface pointer from an object. In my use case this function always results in a nullptr result, even though in the debugger I can see the interface I need nested in my UClass struct.

I took a look at the code in InterfaceCast<> which calls into UObjectBaseUtility::GetInterfaceAddress. The issue is here:

// Script interface
if ( !InterfaceClass->HasAnyClassFlags(CLASS_Native) )
{

So if you have an interface that is defined in code you do not even bother to check to see if there is an implementation provided by script. I copied and modified this code, removing the CLASS_Native check and making it so that the // script interface query only returns a valid interface if can match to an interface with bImplementedByK2 set. Having done this, everything works as expected.

This code change is seems kosher and it enables the desired ability to define a code interface, perhaps even with a default implementation, and to be able to specialise it in blueprints. Mix-in magic.

So I guess the question is: Is the current state of the code an oversight/bug or is it a feature? I could well imagine the code is in this shape because modifying it in this way creates the potential for interfaces which have mixed native and blueprint implementations and maybe there are assumptions made elsewhere in the code that an interface will either be 100% native or 100% blueprint? This is just a guess though - maybe it’s fine?!

Please let me know!

I was aware of and had inspected the code for ScriptInterfaceCast before posting. ScriptInterfaceCast is no more than a convenience for wrapping the result of InterfaceCast in a TScriptInterface<> object. Since InterfaceCast<> fails in my use case, so too will ScriptInterfaceCast<>.

Hi ,

Are you still having trouble with this in version 4.6? InterfaceCast has been deprecated in this version, and we recommend using Cast or dynamic_cast instead.

Hi ,

We have not heard back from you for a little while. Do you still need any assistance with this issue? I will be marking this issue as resolved for tracking purposes, but if you still need help please feel free to re-open it.