How to do it...

  1. Open your game mode's declaration and add a new property to the class:
UCLASS()
class CHAPTER_07_API AChapter_07GameModeBase : public AGameModeBase
{
GENERATED_BODY()

public:
virtual void BeginPlay() override;

TArray<IMyInterface*> MyInterfaceInstances;
};
  1. Add #include "MyInterface.h" to the header's include section:
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyInterface.h"
#include "Chapter_07GameModeBase.generated.h"
  1. Add the following within the game mode's BeginPlay implementation:
for (TActorIterator<AActor> It(GetWorld(), AActor::StaticClass()); 
It;
++It)
{
AActor* Actor = *It;

IMyInterface* MyInterfaceInstance = Cast<IMyInterface>(Actor);

// If the pointer is valid, add it to the list
if (MyInterfaceInstance)
{
MyInterfaceInstances.Add(MyInterfaceInstance);
}
}

// Print out how many objects implement the interface
FString Message = FString::Printf(TEXT("%d actors implement the
interface"), MyInterfaceInstances.Num());

GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, Message);
  1. Since we are using the TActorIterator class, we will need to add the following #include to the top of our GameModeBase class' implementation file:
#include "Chapter_07GameModeBase.h"
#include "MyInterface.h"
#include "SingleInterfaceActor.h"
#include "EngineUtils.h" // TActorIterator
  1. If you haven't done so already, set the level's game mode override to your game mode, then drag a few instances of your custom Interface-implementing actor into the level.
  2. When you play your level, a message should be printed on screen that indicates the number of instances of the interface that have been implemented in Actors in the level: