How to check if actor implements blueprint interface from C++?

Hey all.

I’ve got a use-case where I want to check if an actor has implemented a blueprint interface and then call an interface method if they have from my C++ code. Anyone got any leads on how to do such a thing?

I specifically don’t want to have to cast actors to base types in this process, so I really want to do what in blueprint would be a “does implement interface” node and then a “call function” node.

Thanks for any help.

Turns out I can just create a blueprintimplementableevent and call it and it will fail silently if it hasn’t been implemented, which is what I wanted:

UFUNCTION(BlueprintImplementableEvent)
virtual void HandleNonBlockingHit(const FHitResult &hit, const FVector & start, const FVector & end) const;

That would only work for C++ interfaces though. I’m talking about blueprint interfaces. I need the equivalent of “Does implement interface” and some method of calling the interface function if the object does implement the interface. The point here is to not have to know the class of the thing implementing the functionality.

1 Like

Is this definitely the case?
When I was experimenting with interfaces not long ago, it seemed there were some limitations, in that Cast<> would only work if the object was of a blueprint derived from a C++ class which itself implemented the interface. If the interface was only implemented at the blueprint level (ie. the parent C++ class of the blueprint didn’t implement the interface), Cast<> would return a null pointer.

There was a workaround for calling interface methods but it was very nasty, and did not actually allow getting an interface pointer to the object.


Found the article: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums
It’s a bit outdated, but as far as I could tell, this was still an issue in 4.6 at least.

What about… calling DoesImplementInterface?


UKismetSystemLibrary::DoesImplementInterface( MyObject, UMyInterface::StaticClass() );

3 Likes

This is exactly what I needed, thanks! My goal was to find out if a Blueprint (AnimBlueprint in my case) implemented a C++ Interface.
If you don’t want to include the UKismetSystemLibrary, what the function does is, it checks whether the given Interface StaticClass is actually an Interface and then it calls:


MyObject->GetClass()->ImplementsInterface(UMyInterface::StaticClass());

So you could just call this line if you are sure UMyInterface is an Interface.

4 Likes