How to add default functions to c++ interface

Hi, I’m wondering how I could add default functionality to a C++ interface. For example, I have created this interface
(header)

class My_API IInteractableInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	// Called when the object is interacted with
	UFUNCTION(BlueprintNativeEvent, Category = "Default")
	void OnInteracted(APlayerBase* player, EInteractionType interaction_type);
};

(source)

// Add default functionality here for any IInteractableInterface functions that are not pure virtual.

void IInteractableInterface::OnInteracted(APlayerBase* player, EInteractionType interaction_type)
{
	// I want to put default behavior here
}

With this setup I get the error

error LNK2005: “public: void __cdecl
IInteractableInterface::OnInteracted(class
APlayerBase *,enum EInteractionType)”
(?OnInteracted@IInteractableInterface@@QEAAXPEAVAPlayerBase@@W4EInteractionType@@@Z)
already defined in
Module.MyProject.cpp.obj

I also tried added “_Implementation” to the end of the function in the source file but I get this error:

error C2084: function ‘void
IInteractableInterface::OnInteracted_Implementation(APlayerBase
*,EInteractionType)’ already has a body

Any ideas? Thanks!

AH the class Interface already has a body. So the issue is, UHT creates a default body. What you could try is:

 UFUNCTION(BlueprintNativeEvent, Category = "Default")
     void OnInteracted(APlayerBase* player, EInteractionType interaction_type);
     virtual void OnInteracted_Implementation(APlayerBase* player, EInteractionType interaction_type);

in your header. But if that doesn’t work, then i have no clue. Normally you not have a default implementation for a BPNativeEvent inside an Interface. Well i have never found the need to do so. I do have defaults inside the interface for normal functions. Hopefully this answers your question.

1 Like

In UE4 interfaces can not have a function definition, the point of interface is to classes agree to use common function and variables so they can be contained in common type.

This is not just UE4, most languages with native support of interfaces won’t let you define functions. That said, C++ does not support interfaces natively, instead it support multi-parenting and classes may act as conventional interfaces instead. UE4 UHT keep the guard on that convention (not to mention Blueprint VM probably don’t know how to call a function on interface), but you don’t need to use UFUNCTION and define the function, ofcorse this will work only with C++ not in blueprints

Hey, thank you. This worked for me but I added _Implementation at the end of it. So altogether it looked like this:

// Called when the object is interacted with
UFUNCTION(BlueprintNativeEvent, Category = "Default")
void OnInteracted(APlayerBase* player, EInteractionType interaction_type);
virtual void OnInteracted_Implementation(APlayerBase* player, EInteractionType interaction_type);

Thanks for your help!

Hey, thanks for your reply. See my comment above. I was able to do it using that method.

yeah sorry was a typo on my behalf. Ill fix my answer. Was late :slight_smile: