Checking if a class implements a UInterface

Follow the first two recipes so that you have a UInterface we can check for, and a class implementing the interface, which can be tested against.

How to do it...

  1. Inside your Game Mode implementation, add the following code to the BeginPlay function:
    FTransformSpawnLocation;
    ASingleInterfaceActor* SpawnedActor = GetWorld()->SpawnActor<ASingleInterfaceActor> (ASingleInterfaceActor::StaticClass(), SpawnLocation);
    if (SpawnedActor->GetClass()->ImplementsInterface(UMyInterface::StaticClass()))
    {
      GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, TEXT("Spawned actor implements interface!"));
    }
  2. Given that we are referencing both ASingleInterfaceActor and IMyInterface, we need to #include both MyInterface.h and ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.