C++ interface implemented in blueprint fails cast

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!

2 Likes

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

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

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.

1 Like

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

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

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

1 Like