# C++ interface implemented in blueprint fails cast

**URL:** <https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138>\
**Category:** UI\
**Tags:** interface, cast-failed, unreal-engine, cast, CPP, question, Blueprint\
**Created:** [November 30, 2015, 5:23pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138 "2015-11-30T17:23:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeavyBullets](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HeavyBullets](https://forums.unrealengine.com/u/HeavyBullets)\
**Post date:** [November 30, 2015, 5:23pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/1 "2015-11-30T17:23:10Z")

</div>

Hi there guys, here’s the problem.

I have an interface created like this.

```
UINTERFACE()
class MYPROJECT_API UInteractableInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
	
	
};

class MYPROJECT_API IInteractableInterface
{
	GENERATED_IINTERFACE_BODY()
public:
	UFUNCTION(BlueprintNativeEvent)
	void Interact();
	UFUNCTION(BlueprintNativeEvent)
	void OnStartFocus();
	UFUNCTION(BlueprintNativeEvent)
	void OnEndFocus();
};

```

This work and i can implement the interface in blueprints. The problem comes when i try to cast a blueprint that implemented the interface.

```
if (GetWorld()->LineTraceSingleByChannel(outHit, start, end, ECC_Pawn, collisionQueryParams))
	{
		TArray<AActor*> overlapingActors;
		GetPawn()->GetOverlappingActors(overlapingActors);
		bool isInteractable = outHit.GetActor()->Implements<UInteractableInterface>();
		bool focusedObjectIsOverlaping = overlapingActors.Contains(outHit.GetActor());

		GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, isInteractable ? "YES" : "NO");
		GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Blue, outHit.GetActor()->GetName());
		if (isInteractable && focusedObjectIsOverlaping)
		{
			IInteractableInterface *focusActor = Cast<IInteractableInterface>(outHit.GetActor());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, focusActor != nullptr ? "CASTED" : "CAST FAILED");
			if (this->_interactableTarget != focusActor)
			{
				//End focus if another object was being focused
				//if(this->_interactableTarget != nullptr)
				//	this->_interactableTarget->OnEndFocus();

				//focusActor->OnStartFocus();
				
				//Set the new interactable target and it's editor counterpart
				//this->_interactableTarget = focusActor;
				//this->InteractableTarget = focusActor;
			}
		}
	}

```

Here… the outHit.GetActor() has an object that implements the interface VIA BLUEPRINT…  
When i use the method Implements, it returns true, but when casting to IInterface fails, i read that this is because the Blueprint class has no c++ header information…

So my question would be.

How do i resolve this issue?  
If i get to cast to the interface… how do i expose that IInterface variable to the Blueprint Editor? UPROPERTY seems to fail everytime (won’t let me compile)

Greetings!

---

<div class="post-metadata">

**Author:** ![ZeroParadigm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ZeroParadigm](https://forums.unrealengine.com/u/ZeroParadigm)\
**Post date:** [September 23, 2016, 7:03pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/2 "2016-09-23T19:03:43Z")

</div>

I know this is an old thread question, but if you have found the solution to this problem can you post? Thanks!

---

<div class="post-metadata">

**Author:** ![ZeroParadigm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ZeroParadigm](https://forums.unrealengine.com/u/ZeroParadigm)\
**Post date:** [September 23, 2016, 8:42pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/3 "2016-09-23T20:42:00Z")

</div>

If anybody else stumbles across this issue. A solution and explanation can be found on this awesome post by Xarol

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

---

<div class="post-metadata">

**Author:** ![Sn\_a\_ke](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sn\_a\_ke](https://forums.unrealengine.com/u/Sn_a_ke)\
**Post date:** [January 17, 2018, 4:13pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/4 "2018-01-17T16:13:52Z")

</div>

why is this not considered a bug and does not need fixing?  
I have not found any information that is going to fix it, or that it is generally considered a bug.

---

<div class="post-metadata">

**Author:** ![HeavyBullets](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HeavyBullets](https://forums.unrealengine.com/u/HeavyBullets)\
**Post date:** [January 17, 2018, 5:16pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/5 "2018-01-17T17:16:28Z")

</div>

the point is that (now that i’m a little more experienced in how to use interfaces) is that you should NOT use IInterface except for making calls… if you want to store the object that is of an interface type, you should use TScriptInterface and call IMyInterface::Execute\_Something(myTScriptInterface, someArgs);

not at all that cute, but it works as intended

---

<div class="post-metadata">

**Author:** ![Sn\_a\_ke](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sn\_a\_ke](https://forums.unrealengine.com/u/Sn_a_ke)\
**Post date:** [January 17, 2018, 8:50pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/6 "2018-01-17T20:50:19Z")

</div>

thanks for your reply.

really, I still studied how to work with interfaces,  
and I was told about to TScriptInterface.

and you are now confirmed this as the right direction.  
thank you!

something like this:

```
TScriptInterface<IMyInterface> MyInterface;
...
//you still need to check if the object has an interface
//you can specify any object
//but if you call interface methods - this leads to a crash of the engine
//if the object does not have this interface
if (ActorInstance->GetClass()->ImplementsInterface(UMyInterface::StaticClass()))
{
	MyInterface.SetObject(ActorInstance);
}
...
MyInterface->Execute_MyInterfaceFunction(ActorInstance);
//or
if (MyInterface.GetObject())
{
	MyInterface->Execute_MyInterfaceFunction(ActorInstance);
}
//or 
IMyInterface::Execute_MyInterfaceFunction(ActorInstance);
```

---

<div class="post-metadata">

**Author:** ![HeavyBullets](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HeavyBullets](https://forums.unrealengine.com/u/HeavyBullets)\
**Post date:** [January 17, 2018, 8:52pm UTC](https://forums.unrealengine.com/t/c-interface-implemented-in-blueprint-fails-cast/340138/7 "2018-01-17T20:52:16Z")

</div>

yes, it sounds weird first… but when you factor the fact that there exists blueprint too, and they also can be called… or there are native or blueprintimplementable method… this is the correct way to handle it
