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.
// 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.");
}
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
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.
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?