# Casting to a UInterface implemented in native code

**URL:** <https://forums.unrealengine.com/t/casting-to-a-uinterface-implemented-in-native-code/489397>\
**Category:** UI\
**Tags:** casting, game-mode, interfaces, unreal-engine, UI, CPP, question\
**Created:** [September 30, 2021, 11:08pm UTC](https://forums.unrealengine.com/t/casting-to-a-uinterface-implemented-in-native-code/489397 "2021-09-30T23:08:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_e8e17e55](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e8e17e55](https://forums.unrealengine.com/u/anonymous_user_e8e17e55)\
**Post date:** [September 30, 2021, 11:08pm UTC](https://forums.unrealengine.com/t/casting-to-a-uinterface-implemented-in-native-code/489397/1 "2021-09-30T23:08:09Z")

</div>

What does this topic explains?  
Actually I am studying unreal engine with “Ue4 Scripting with c++ cookbook” by PACKT publication.  
And I can’t understand what this topic is trying to explain…

See the topic under this heading “Casting to a UInterface implemented in native code”

_ **Casting to a UInterface implemented in native code** _

One advantage that UInterfaces provide you with as a developer is the ability to treat a collection of heterogeneous objects that implement a common interface as acollection of the same object, using Cast\< \> to handle the conversion.

Getting ready 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 reuse a project and GameMode from a previous recipe.  
How to do it…  
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;
};

```

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"

```

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);

```

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 "EngineUtils.h" 

```

// TActorIterator If you haven’t done so already, set the level’s game mode override to your5.  
game mode, then drag a few instances of your custom Interfaceimplementing actor into the level.  
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:
