Cannot cast to C++ interface in Blueprints
While I can test if a particular object implements an Interface in blueprints I cannot cast that object to that interface and use it as that interface. Under the circumstances I am overriding functions within the blueprints I will NOT have the concrete version of that object, they are being sent in as base objects such as AActor or UObject.
I'm building the system of interfaces to orchestrate how seemingly unrelated Actor objects communicate with each other. I've build the interface functions to take in any AActor object and test of that Actor has the appropriate interface for performing actions on. This works beautifully in C++. However Blueprint seems to be a different beast.
So Lets say I have two Interfaces:
So InteractingActor performs a scan for anything in view. If it finds an actor that is any Actor in view and attempts to it to the InteractingAgent's PendingInteractable (which is an Actor pointer) using the function SetPendingInteractable and passes that Actor into that function where it tests if that Actor has the IInteractableActor Interface.
Then if the user presses the interact button, the InteractingAgent's Interact function is fired passing the PendingInteractable into that function where it is cast once again to an IInteractingAgent using an interface cast where it's interact function is called passing the Pawn in as the instigator as an actor which it is then interface ast
Basically I have certain functions that will be overriden in blueprint. Because the interactions can happen between Actor and Actor or Actor and Pawn, or UObject and Actor and Pawn. Since these are the basic Subclasses of all UE4 objects the interfaces expect the least common denominator of the three (somethings will never be on things that are not actors so the least common denominator is Actor). So in blueprint I need away to cast an UObject, Actor, or Pawn to the desired Interface to work with it... however that functionality does not seem to exist...
Technically I could Cast to every Base Class of my own within blueprints but then that somewhat defeats the entire purpose for using Interfaces...
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.
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.
Ok, so the issue here is that you aren't marking your functions up correctly so that they can be exposed to Blueprint. Once that is done you will see some Interface Message nodes that show up in the context menu that will transparently handle casting to the interface class and calling functions from it.
Declare your functions like so:
If you want to define a default implementation in native code you should take a look at BlueprintNativeEvent instead of BlueprintImplementableEvent:
Note the absence of virtual - marking the function up as Implementable involves the generated code including a stub that calls the correct Blueprint functionality.
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 https://answers.unrealengine.com/questions/214147/grand-unified-cblueprint-cast-interface-explanatio.html in case it would be helpful.
answered Apr 18 '15 at 07:01 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here