Check if Actor implements an interface

How can I check if an Actor implements a specific blueprint interface? For example, I want to store a reference to an actor in a variable returned from an EventBeginOverlap, then call the interface functions in the other actor later on. But I need to first check if the actor given from the event implements the interface.

Thanks.

Use ā€œCall Function/Utilities/Does Implement Interfaceā€.

Best regards.

1 Like

In Blueprints its save to call Interface Functions without checking first. But be aware that using any Outputs afterwards will default/null if it was a Object without Interface. In that case you want to check manually first before proceding.

1 Like

Find from other site.
UKismetSystemLibrary::DoesImplementInterface( MyObject, UMyInterface::StaticClass() );

Simplest C++ method Iā€™ve found for checking Interface implementation:

MyObject->GetClass()->ImplementsInterface(UMyCoolInterface::StaticClass())
6 Likes

UObject has an Implements() method, hence it can be simplified to

MyObject->Implements<UMyCoolInterface>();
1 Like

Thanksā€¦ Chat failed hereā€¦
:wink:

To check if an Actor implements a specific blueprint interface, you can use the GetComponentsByInterface function. This function takes an interface as a parameter and returns an array of all the components that implement that interface.

Hereā€™s an example of how you can use GetComponentsByInterface to check if an Actor implements a specific blueprint interface:

// Get a reference to the Actor that triggered the EventBeginOverlap
AActor* OverlapActor = GetComponent<UActorComponent>()->GetOwner();

// Get a reference to the blueprint interface you want to check for
TSubclassOf<UBlueprintInterface> InterfaceClass = TSubclassOf<MyBlueprintInterface>();

// Get all the components that implement the interface
TArray<UActorComponent*> InterfaceComponents = OverlapActor->GetComponentsByInterface(InterfaceClass);

// Check if any of the components implement the interface
if (!InterfaceComponents.IsEmpty())
{
    // The Actor implements the interface, you can now use the interface functions in the other actor
    // ...
}
else
{
    // The Actor does not implement the interface, you can handle this case as needed
    // ...
}

In this example, MyBlueprintInterface is the blueprint interface you want to check for. You can replace it with the name of the blueprint interface you want to check for.

Note that GetComponentsByInterface returns an array of all the components that implement the interface, so you can iterate over the array to check if any of the components implement the interface.