Interface problem in 4.16

Hi,

I cannot get to compile my code, because I’m fighting with the Interface system.

The only difference in my method is that now I am using the “UnrealInterface” as the base class, instead of an empty class.

I only want to use the Interface in C++, so I would prefer not to mark my functions as BP Native or Implementable events.

I have created a test project to make things easier.
MyInterface: The Interface.
MyTriggerVolume: The Actor that calls the MyInterface function.
TargetActor: The Actor that implements MyInterface.

Here is my code:

MyInterface.h



#include "CoreMinimal.h"
#include "MyInterface.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI, meta = (CannotImplementInterfaceInBlueprint))
class UMyInterface : public UInterface
{
	GENERATED_BODY()
};


class INTERFACETEST_API IMyInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	
	virtual void RunInterfaceTEST() = 0;
};


MyTriggerVolume.cpp



#include "MyTriggerVolume.h"
#include "MyInterface.h"

void AMyTriggerVolume::OnTiggerOverlap(AActor* InActor)
{
	IMyInterface* MyInterface = Cast<IMyInterface>(InActor);

	if (MyInterface != nullptr)
	{
		MyInterface->Execute_RunInterfaceTEST(InActor);
	}
}


TargetActor.h



#include "MyInterface.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TargetActor.generated.h"

UCLASS()
class INTERFACETEST_API ATargetActor : public AActor, public IMyInterface
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATargetActor();

	virtual void RunInterfaceTEST() override;
};


TargetActor.cpp



void ATargetActor::RunInterfaceTEST_Implementation()
{

}


Compile Error message:



\Documents\Unreal Projects\InterfaceTEST\Source\InterfaceTEST\MyTriggerVolume.cpp(21): error C2039: 'Execute_RunInterfaceTEST': is not a member of 'IMyInterface'
\documents\unreal projects\interfacetest\source\interfacetest\MyInterface.h(19): note: see declaration of 'IMyInterface'
\Documents\Unreal Projects\InterfaceTEST\Source\InterfaceTEST\TargetActor.cpp(15): error C2039: 'RunInterfaceTEST_Implementation': is not a member of 'ATargetActor'
\documents\unreal projects\interfacetest\source\interfacetest\TargetActor.h(11): note: see declaration of 'ATargetActor'
1>ERROR : UBT error : Failed to produce item: \Documents\Unreal Projects\InterfaceTEST\Binaries\Win64\UE4Editor-InterfaceTEST.pdb
1>  Total build time: 17.16 seconds (Local executor: 0.00 seconds)
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets(41,5): error MSB3075: The command ""C:\Program Files\Epic Games\UE_4.16\Engine\Build\BatchFiles\Build.bat" InterfaceTESTEditor Win64 Development "\Documents\Unreal Projects\InterfaceTEST\InterfaceTEST.uproject" -waitmutex" exited with code 5. Please verify that you have sufficient rights to run this command.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I have tried several other solutions found on the net, but apparently none of them worked.

At this point I would ask if anyone knows the correct way to create, implement and call a C++ only interface?

Thanks!

This is how you should execute interface methods:

if (InActor->Implements<UMyInterface>())
{
IMyInterface::Execute_RunInterfaceTEST(InActor);
}

Hi Iceey,

Thanks for your solution, I have never seen this Implements check before. Are you sure that the casting I have used is incorrect? It was working well before 4.16.

Unfortunately, changing the interface calling method produces the same error :frowning:

I have never had a problem with interfaces before, and it is driving me crazy now. Is it even possible to work with the UnrealInterface derived interface?

Any input would be really appreciated!

Sorry to hear that it didn’t work. I always create my interfaces through the editor by clicking Add new c++ class. My interfaces are purely virtual and I overwrite the behaviour in C++ class aswell as derrived BP Here’s a sample so you can copy it:
I also noticed that you use GENERATED_BODY(). Try using GENERATED_UINTERFACE_BODY() aswell as GENERATED_IINTERFACE_BODY(). Hope it helps


#pragma once

#include "InterfaceInventory.generated.h"

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

/**
 * 
 */
class AOH_API IInterfaceInventory
{
	GENERATED_IINTERFACE_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Ownership")
		void OnChange_ItemArray();
	
};



#include "InterfaceInventory.h"


// This function does not need to be modified.
UInterfaceInventory::UInterfaceInventory(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}

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

Then in C++ base class:


#include "InterfaceInventory.h"

UCLASS()
class AOH_API ADefaultMapCharacter : 
	public ACharacter,
	public IInterfaceInventory
{
	GENERATED_BODY()

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Character Properties")
		void OnChange_ItemArray();
	virtual void OnChange_ItemArray_Implementation() override;

}


void ADefaultMapCharacter::OnChange_ItemArray_Implementation()
{

}

I do Implements checks to inform my log that some actor didn’t declare it’s behaviour. My system is based on Component <-> Interface combination. Some of my components also require their Interfaces to be implemented too. Saves a lot of time debugging.

Thanks Iceey, I will be soon able to test it out!

I noticed that macro difference too, but this is what Unreal has generated. There is a class called UnrealInterface that you can inherit from (using the Editor’s AddClass tab), and I was able to use it with BlueprintNative events. However I want to use pure virtual functions this time, but I cannot get them to compile. I guess I will stick to the “traditional” interface creation method, instead of this new class.

Thanks again!

Okay, I finally got it to working, thank you so much for your help Iceey!

I managed to used the UnrealInterface class, here is what I had to change based on my original post, if anyone is interested.

MyInterface.h



#include "CoreMinimal.h"
#include "MyInterface.generated.h"

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


class INTERFACETEST_API IMyInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	
        UFUNCTION(BlueprintNativeEvent)
	void RunInterfaceTEST();
};


MyTargetActor.h



#include "MyInterface.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TargetActor.generated.h"

UCLASS()
class INTERFACETEST_API ATargetActor : public AActor, public IMyInterface
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATargetActor();

	virtual void RunInterfaceTEST_Implementation();
};


The weird thing is that the IMyInterface::Execute_ method was crashing the game, so I had to use my original solution.

That was it, I never did, and would have thought about using _Implementation in the .h declaration, so thank you so much again, Iceey!

No problem. Glad I could help.