Calling Interface Functions In C++

I came across this topic when trying to solve a specific UInterface related issue and I thought I might add one important thing to the discussion.

There are actually 2 scenarios in which we’re calling UInterface functions in C++:

  1. When interface is inherited by C++ class
  2. When interface is added in the blueprint Class Settings

Each of those requires different handling in C++ when calling an interface function.
First scenario:

if (Cast<IInterface_MyInterface>(MyActor))
{
	Cast<IInterface_MyInterface>(MyActor)->Execute_MyInterfaceFunction(MyActor);
}

Second scenario (Note UInterface in the if statement and IInterface in the function call!):

if (MyActor->GetClass()->ImplementsInterface(UInterface_MyInterface::StaticClass()))
{
	IInterface_MyInterface::Execute_MyInterfaceFunction(MyActor);
}

Perhaps someone will find this information useful.

6 Likes