One advantage that UInterfaces provides you with as a developer is the ability to treat a collection of heterogeneous objects that implement a common interface as a collection of the same object, using Cast< >
to handle the conversion.
You should have a UInterface
, and an Actor
implementing the interface ready for this recipe.
Create a new game mode using the wizard within Unreal, or optionally, reuse a project and GameMode
from a previous recipe.
Open your game mode's declaration, and add a new
UPROPERTY() macro
to it:UPROPERTY() TArray<IMyInterface*>MyInterfaceInstances;
Add
#include "MyInterface.h"
to the header's include section.Add the following within the game mode's
BeginPlay
implementation:for (TActorIterator<AActor> It(GetWorld(), AActor::StaticClass()); It; ++It) { AActor* Actor = *It; IMyInterface...