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...
- 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!")); }
- Given that we are referencing both
ASingleInterfaceActor
andIMyInterface
, we need to#include
bothMyInterface.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.