UINTERFACE

Please, can someone provide the minimum code needed for creating (and using) an interface? I’ve been trying to find info about it for days, but all I get is errors when I try to compile the code there is on the web. Most of it is probably outdated, too.

For starters, I just want a common interface for pawns and characters to process input from the character controller. If I could, I would just derive a class from pawn, which has all the needed functions, and then derive the default character from that custom pawn. I don’t want to modify the engine though, so interfaces seem to be the way to go here.

This is the header:

#pragma once

#include "ControllerInterface.generated.h"

UINTERFACE()
class UControllerInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()

public:
	UControllerInterface(const FObjectInitializer& ObjectInitializer);
};

class Project01_API IControllerInterface
{
	GENERATED_IINTERFACE_BODY()

public:

	virtual void MoveStraight(const float Val);
	virtual void MoveSide(const float Val);
	virtual void MoveVertical(const float Val);
};

And this is the .cpp:

#include "Project01.h"
#include "ControllerInterface.h"

UControllerInterface::UControllerInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{}

void IControllerInterface::MoveStraight(const float Val)
{}
void IControllerInterface::MoveSide(const float Val)
{}
void IControllerInterface::MoveVertical(const float Val)
{}

error for UINTERFACE(): “This declaration has no storage class or type specifier”

for #include “ControllerInterface.generated.h”: “Cannot open source file “…””

I think I’ve got it now, at least it compiles…

It seems that I got all kinds of errors in different places until I removed the “Project01_API” from the IInterface declaration. Now it looks good and I will try to actually use the interface in another class.

Resolved by removing the Project01_API from the IInterface declaration.