Problem with passing IInterface as a method parameter

I have a plugin with a C++ interface - IMyInterface and a C++ class - UMyActor.
In my main project I added my plugin and I have made two blueprints, one BP_MyActor
which extends the C++ plugin UMyActor, and another BP_MyWidget that extends UMG’s UWidget, which gets added to viewport when the level loads. BP_MyActor is also part of the level, and BP_MyWidget has an object reference to BP_MyActor from the level.

The code looks as following:

// IMyInterface
class MYPLUGIN_API IMyInterface
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintImplementableEvent)
	void TriggerAnEvent(UObject* WrapperObject);	
};
// UMyActor
UCLASS()
class MYPLUGIN_API UMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	UFUNCTION(BlueprintCallable)
	void DoSomething(TScriptInterface<IMyInterface> InterfaceObject)
	{
		UObject* Something = NewObject<UObject>();
		// do something with an object here
		InterfaceObject->TriggerAnEvent(Something);
	}
};

I added IMyInterface to BP_MyWidget as interface, and through blueprints Event Graph editor I have implemented TriggerAnEvent and DoSomething, but when I run it, it throws me an exception:

void IMyInterface::TriggerAnEvent(UObject* WrapperObject)
{
	check(0 && "Do not directly call Event functions in Interfaces. Call Execute_TriggerAnEvent instead.");
}

How can I solve this?

Hello :slight_smile:

Short answer:
You should do exactly what exception tells you. Instead of:

InterfaceObject->TriggerAnEvent(Something);

Your code should be like that:

IYourInterface::Execute_TriggerAnEvent(InterfaceObject.GetObject(), Something);

GetObject is necessary in this case because TScriptInterface is essentially a wrapper for a UObject that points to an interface object. BUT after reading the post below you should be able to find better way to handle this interface usecase :smiley:

This has something to do with how Unreal handles interfaces and the ability to implement them in BPs, and that way the method will always work whether the interfacce was implemented in C++ or BP.

Long answer:
If you will be working with interfaces in UE check out this link it should have everything and more of what you would need.

Edit: Fixing the method call, and providing some extra info. Little late update but I can’t leave it like that if it’s marked as solution.

1 Like

Hello, thanks for the reply. Article is really good, I’ll check it in more details.
However, if I put:

instead of:

then I get compilation errors.

Even if I try with InterfaceObject->Execute_TriggerAnEvent(InterfaceObject, Something), I still get compilation errors as Execute_TriggerAnEvent expects by definition following format: Execute_TriggerAnEvent(UObject *, UObject *), because IntefaceObject doesn’t inherit from UObject.

Do I perhaps need to change something in one of my UFUNCTION macros or somehow cast by InterfaceObject to UObject?

I think I might found solution. If I replace definition of my DoSomething method with the following one

void DoSomething(UObject* InterfaceObject);

then I can just

		bool bImpl = UKismetSystemLibrary::DoesImplementInterface(InterfaceObject, UMyInterface::StaticClass());
		if (bImpl)
		{
			IMyInterface::Execute_TriggerAnEvent(InterfaceObject, WrapperObject);
		}

instead of
InterfaceObject->TriggerAnEvent(Something);