c++ Interface

I’m trying to create an interface for my Room classes that will be fired PreSave, PostLoad and when a player has finished loading. So, I implemented an interface for this. I already have the hooks in place and the interface itself is fairly simple.

RSaveInterface.h

#include "CoreMinimal.h"
#include "RSaveInterface.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class URSaveInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class MYPROJECT_API IRSaveInterface
{
	GENERATED_BODY()
	
public:	
	//CPP Function call called by the RSaveComponent before the blueprint PreSave Event
	void PreSave();
	
	//CPP Function call called by the RSaveComponent before the blueprint PostLoad Event
	void PostLoad();
	
	//CPP Function call called by the RSaveComponent before the blueprint PlayerLoaded Event
	void PlayerLoaded(APlayerController* PC, APawn* Pawn, int32 PlayerIndex);
};

RSaveInterface.cpp

#include "RSaveInterface.h"
    
void IRSaveInterface::PreSave()		{}
void IRSaveInterface::PostLoad()	{}
void IRSaveInterface::PlayerLoaded(APlayerController* PC, APawn* Pawn, int32 PlayerIndex)	{}

When I go to implement the interface on my RoomActor I use multiple Inheritance just like the docs say

class MYPROJECT_API ARRoom : public AActor, public IRSaveInterface

But when I try to override the implemented function, I get a bunch of errors:

error C3668: 'ARRoom::PostLoad_Implementation': method with override specifier 'override' did not override any base class methods RSaveInterface.gen.cpp
 error C3668: 'ARRoom::PostLoad_Implementation': method with override specifier 'override' did not oerror C3668: 'ARRoom::PostLoad_Implementation': method with override specifier 'override' did not override any base class methodsverride any base class methods
error C3668: 'ARRoom::PostLoad_Implementation': method with override specifier 'override' did not override any base class methods

etc…

To make your function overridable the function need to be “virtual” telling compiler to use proper function on specific class, if you not gonna use it you can’t override only mask function, in other words add function to class namespace. So do this:

virtual void PreSave();

Virtual functions also don’t need function definition so remove them from cpp file, but function definitions is required if you want to create object direcly out of this class, which in case of interface it won’t happen.

Thanks , This is the first time I’m making a C++ interface for UE4. I’ve gotten by without them for years but it’s a tool I should really have under my belt by now. Interfaces are super powerful and have a million uses.

I did need the function definitions in the end otherwise I got linker errors so I just created some empty void functions I can easily override.

Cheers!

I for got to mention, although C++ don’t directly support abstract classes (same as Interfaces themselves since it multi-parenting support which allows to emulate interfaces in C++), classes with undenefied virtual functions due to behavior i mentioned above are consider “Abstract” and lot of C++ tutorials mentions that. In UE4 you can mark UObject class as abstract by adding “Abstract” specifier in to UCLASS(), which will prevent engine from creating object out od this class and let you use undenefied virtual functions

Yeah I knew about marking a UClass as abstract. Not that the engine assumed abstract though. Good to know.