Cannot cast to C++ interface in Blueprints

After EXTENSIVE research on this matter I finally solved it, though it was far form easy and there still exists some excentricities. Twiddle was not far off, but there was a gap in my own knowledge that was answered by pure luck of me discovering an Unreal Engine Interface that I could hook into through blueprints. After spelunking through the engine code I found the key to this riddle in the IGameplayTagAssetInterface found under the Runtime/GameplayTags/Classes folder.

So This solution is specifically well suited if you have multiple objects utilizing a different base object. In my case the interface will be applied to AActor types and ACharacter types, both share the AActor as a base class which allows my interface to pass round AActor types and InterfaceCast them to the proper interfaces in the implemented of the functions.

MyInterface.h

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

class MYGAME_API IMyInterface
{
    GENERATED_IINTERFACE_BODY();


    UFUNCTION(BlueprintCallable, Category = "My Interface")
    virtual AActor * GetSomeActor();

    UFUNCTION(BlueprintCallable, Category="My Interface")
    virtual bool SomeTestFunction(bool IsSomething);
}

MyInterface.cpp

/// Interface constructor here stuff ...

AActor * IMyInterface::GetSomeActor()
{
    unimplemented();
    return NULL;
}

bool SomeTestFunction(bool IsSomething)
{
    unimplemented();
    return false;
}

MyActor.h

class AMyActor : public AActor, public MyInterface
{
    UFUNCTION(BlueprintCallable, Category = "My Interface")
    virtual AActor * GetSomeActor() override;

    UFUNCTION(BlueprintCallable, Category="My Interface")
     virtual bool SomeTestFunction(bool IsSomething) override;
};

MyActor.cpp

AActor * MyActor::GetSomeActor()
{
   AActor * returnActor = NULL;

   // Some code here to do things and get an actor and set returnActor

    return returnActor;
}

bool MyActor::SomeTestFunction(bool IsSomething)
{
   bool returnBoolean = false;

   // do some stuff and test some stuff set returnBoolean

    return returnBoolean;
}

You can pretty much do anything you want with this setup except the major drawback is that this interface CANNOT be implemented in blueprints. It just so happends that my solution does not need that at the level I am working with. However if you want to overcome that drawback I suggest trying to build a second interface that can be blueprint implementable that is called by your non-blueprint implementable interface

So you’d have MyInterface and MyImplementableInterface that has functions and events that are called from MyInterface (at least from the implementing class).

So now that you have this interface built, your base classes will implement the interface and then from blueprints you will need to call functions from it. In blueprints this is a bit different than I had expected but it works none-the-less. In any blueprint right click (without having any line drawn out) and look for a category called “Interface Messages”. This is where you interfaces functions will be kept under what ever category you gave them.

Below is a screen show of how that will look with a few annotations.

Basically these “Interface Messages” take as the first parameter any object in the game and attempts to cast and call the function from that “target” object. If it fails it does so silently, if it succeeds then all is good. I suggest prefixing any one of these calls with a “Implements Interface” node in case you have any thing down the chain of events that require that that object actually implement the interface.

I hope this helps anyone else out there.

I am glad to hear that you were able to find a solution that works for you, though I still think this should probably work a little more smoothly. I did want to make sure you were aware that the new version 4.6 that was released today does have some improvements specifically related to interfaces and casting:

Simplified Casting

Easily cast between Unreal classes
using the improved Cast() function.
You can even cast between interfaces
and classes. We also now support using
dynamic_cast to cast between Unreal
types, if you prefer that.

and:

InterfaceCast has been deprecated -
Cast or dynamic_cast should be used
instead.

I would certainly be interested in finding out if some of these changes help with the issues you have been experiencing.

,

I would LOVE this to be smoother as well and appreciate any work you guys and gals at epic put into making interfaces (and C++ coding for that matter) a bit more awesome. Interfaces are powerful things and highly useful.

So far my only remaining problem with interfaces is, even though this solution works for now it still has the problem that I cannot implement my interface in blueprints, and I must make a C++ base class for it. It’s a minor irritation but if I am working on a team and my designer is lacking C++ knowledge he cannot prototype anything that uses that interface in Blueprints (with out an already established base class).

I am upgrading my project to 4.6 today and look forward to utilizing the new programming features. Thank you for your help!

: It sounds like ArcainOne is saying that if a Blueprint implements a C++ interface then you can’t cast a AActor* to the interface in C++. Is that right? That seems to be a huge limitation interfaces.

Thanks to both of you.

Hello ,

Nope, you should be fine in C++. Using the Cast(myActor) you can cast an AActor pointer to your interface in C++.

My issue was that once I built an interface in C++, I cannot implement that interface in a new Blueprint Class.

So if I created an MyTestPawn in Blueprints derrived from Pawn I could not add my interface to it. Now I still have not tested this if it is true or not as I am currently in the drudges of coding, not designing. So I may be wrong. the “CannotImplementInterfaceInBlueprint” meta specifier seems to indicate otherwise.

Also the problem you where probably referring was probably the fact that UE4 Editor does not understand the Interface Object. It seems to understand the Interface functions and creates static nodes of those functions. Normally this is not an issue but if you have an Interface function that you want to return different Interface or take in an interface as a parameter, UE4 will not recognize it in the editor, and I think the compile may not work either… been a while since I’ve tried it.

To get around it, any time you want to have a function that takes in or returns another interace object, you need to reduce it to it’s lowest common class within UE4.

As an example you have InteraceA which is only applied to AActor objects. Then you have InterfaceB that only uses objects that implement InterfaceA.

Unfortunatly you can’t write your code as

void MyFunction(IInterfaceA * interfaceA)

you have to write it:

void MyFunction(AActor * interfaceA)

then inside your function check to see if that AActor implements the interface

void IInterfaceB::MyFunction(AActor * interfaceA)
{
    IInterfaceA * myInterfaceA = Cast<IInterfaceA>(interfaceA)
    if(myInterfaceA)
    {
        // do stuff
    }

}

Thanks very much for this question. The question and the many answers helped me a lot with my interface issues and were a key part of me finally understanding what is going on.

I think you don’t need two separate interfaces if you use the Execute_* forms. I tried to summarize what I think is going on with interfaces mixing C++ and Blueprint and why at Grand unified C++/blueprint cast interface explanation - UI - Unreal Engine Forums in case it would be helpful.

Thanks,
-X