Download

Problem: blueprintable interface with default functionality

Hi! I am trying to make C++ interface that is blueprintable. I have couple of actor classes using the interface and I want to share default functionality so I don’t have to implement the same code to every actor separately. I want that functions defined in interface are callable from blueprints. The interface is working fine if I implement functions to my actor by overriding MyFunction_Implementation(). If i don’t implement function to my actor but instead to the interface, compiler throws an error that says MyFunction_Implementation() is abstract and has to be overridden.

So it seems that UE4 header tool creates _Implementation version of the function as abstract. There is a comment in interface when I create it from editor that says “Add default functionality here for any ITestInterface functions that are not pure virtual.”
So the question is: How can I create BlueprintCallable and BlueprintNativeEvent - function to interface that has default implementation in the interface so I don’t have to override it in actor that is using the interface?

Okay, I made a little “hack” to make this work. It seems that it is only possible to inherit functionality from interface via “normal C++ multi-inheritance”, correct me if I am wrong. I created two functions to the interface to achieve this, one that is called from blueprint and one that is called from implementation. That way I still have to override implementation function in actor but I can keep it simple by calling the second function that executes the actual logic:

Interface.h



UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Actions")
bool SelfDestruct(); // This is function visible to blueprints
static bool SelfDestruct_Call(class UObject* obj); //This executes actual logic


Interface.cpp



bool IIActionable::SelfDestruct_Call(UObject* obj)
{
	UE_LOG(LogTemp, Warning, TEXT("Call worked"));
	return true;
}


Actor.h



bool SelfDestruct_Implementation() override;


Actor.cpp



bool ATActiveActor::SelfDestruct_Implementation()
{
	return IIActionable::SelfDestruct_Call(this);
}