How to create a UINTERFACE

Creating a trivial interface seen by the Unreal Editor is literally driving me crazy…

I googled a lot and found nothing useful for 4.7.
Can anyone tell me how the hell do I create an interface? It’s a foundamental thing in programming, it’s not acceptable to renounce using it!

I already read this tutorial and it’s extremely easy as you say. The problem is that from the editor I cannot create a new class inheriting from UInterface so the .generated.h defining the UINTERFACE() macro is not created and if I extend simply UObject in order to edit it later the .generated.h does not contain the macro, instead has #undef UINTERFACE

THAT is the real problem, setting up the two classes needed is pretty straightforward

Yeah, creating Interface is not as easy as it should (meaning, it should be simpler) but it’s still pretty easy :

Here is how I implemented one of mine :

UINTERFACE(MinimalAPI)
class UMyCheckpoint : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class MYGAME_API IMyCheckpoint
{
	GENERATED_IINTERFACE_BODY()

	virtual void SaveCheckpoint(int8 aCheckpointID) = 0;
	virtual void LoadCheckpoint(int8 aCheckpointID) = 0;
};

There is some things to know about IInterface and UInterface, I think Rama explain it best here : A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

Hope this helps!

Mick

Ok, don’t worry, it’s pretty easy :slight_smile: Yes the tutorial do not mention how, but it automatically generates once you compile. I was asking myself the same question when first created it. Here is my COMPLETE .h :
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "JotunnCheckpoint.generated.h"


UINTERFACE(MinimalAPI)
class UJotunnCheckpoint : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class MYGAME_API IJotunnCheckpoint
{
	GENERATED_IINTERFACE_BODY()

	virtual void SaveCheckpoint(int8 aCheckpointID) = 0;
	virtual void LoadCheckpoint(int8 aCheckpointID) = 0;
};

And my complete .cpp:

// Fill out your copyright notice in the Description page of Project Settings.

// Fill out your copyright notice in the Description page of Project Settings.

#include "Jotunn.h"
#include "JotunnCheckpoint.h"

UJotunnCheckpoint::UJotunnCheckpoint(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{

}

Just create a new class using UObject, then replace for UInterface.

Replace the MYGAME_API for yours (if your project is name MyGreatGame, it should be MYGREATGAME_API )

After that compile your game in Visual Studio and UBT will create the .generated.h for you.

Well you sure helped, I would never imagine that the code would be generated at compile time!! Thank you very much!

Unfortunately I encountered other errors… If I don’t ask too much can you please give me a hint?

here is the .h

#pragma once

#include "GenericSwitch.generated.h"

UINTERFACE(MinimalAPI)
class UNIDUNGEON_API UGenericSwitch : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class UNIDUNGEON_API IGenericSwitch
{
	GENERATED_IINTERFACE_BODY()
public:
	/*UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Switch)
		int32 statesCount = 2;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Switch)
		int32 currentState = 0;

	UFUNCTION(BlueprintCallable, Category = Switch)
		virtual void Increment();
	UFUNCTION(BlueprintCallable, Category = Switch)
		virtual void Decrement();*/

	virtual int32 GetStatesCount() = 0;
	virtual int32 GetCurrentState() = 0;
};

here are the compiler errors:

Error 1 error C2487: ‘GetPrivateStaticClass’ : member of dll interface class may not be declared with dll interface d:\unrealengine\unidungeon\source\unidungeon\GenericSwitch.h 13 1 UniDungeon
Error 3 error C2487: ‘GetPrivateStaticClass’ : member of dll interface class may not be declared with dll interface D:\UnrealEngine\UniDungeon\Source\UniDungeon\GenericSwitch.h 13 1 UniDungeon
Error 2 error C2487: ‘UGenericSwitch::{ctor}’ : member of dll interface class may not be declared with dll interface d:\unrealengine\unidungeon\source\unidungeon\GenericSwitch.h 13 1 UniDungeon
Error 4 error C2487: ‘UGenericSwitch::{ctor}’ : member of dll interface class may not be declared with dll interface D:\UnrealEngine\UniDungeon\Source\UniDungeon\GenericSwitch.h 13 1 UniDungeon

Great job!!
I’m now seing further errors because my project’s generated cpp file was declaring a constructor for my interface (I initially created the class as a Ubject, i guess that’s the reason).

Thank you very much for your help!!!

Ha, nice one, I had my project with my working interface in 4.7.1, since you had this issue I went ahead and tried to do the same in 4.7.5, and it did the same error as you! After some messing around, I found that the “MinimalAPI” in UINTERFACE was the issue. Looks like they changed that somewhere along the way.

So just use UINTERFACE() instead of UINTERFACE(MinimalAPI)

You can accept my answer than :slight_smile: Have a nice day!