Download

Generating headers

Hi,
I tried to follow this https://wiki.unrealengine.com/Interfaces_in_C%2B%2B to create interface etc. It seems to be generated but I can’t figure it out how to generate a interface. I can Add some basic code to project trough the editor but not the interface for example.

hm I’m successfully working with interfaces… please show some code so that we can help you.

Basically it’s like this:



//this is in 	MyInterface.h

#include "MyInterface.generated.h"
 
UINTERFACE()
class UMyInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};
 
class IMyInterface
{
	GENERATED_IINTERFACE_BODY()
 	[function declarations]
};




//this is in 	MyInterface.cpp

#include "MyInterface.h"
 
IMyInterface::MyFunction(){}
// function bodies if needed





//this is in 	SomeClass.h

#include "SomeClass.generated.h"
 
UCLASS()
class USomeClass : public UObject, public IMyInterface



Are you creating classes in visual studio just by adding new classes or UE4 editor to add code to project?

Header:



// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.
 
#pragma once
 
#include "UDanceInterface.generated.h"
 
/** Class needed to support InterfaceCast<IToStringInterface>(Object) */
UINTERFACE(MinimalAPI)
class UDanceInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};
 
class IDanceInterface
{
	GENERATED_IINTERFACE_BODY()
 
	virtual FString ToString();
};




cpp:



#include "DanceInterface.h"

//////////////////////////////////////////////////////////////////////////
// ToStringInterface

UDanceInterface::UDanceInterface(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{

}

//This is required for compiling, would also let you know if somehow you called
//the base event/function rather than the over-rided version
FString IDanceInterface::ToString()
{
	return "IDanceInterface::ToString()";
}



And something that uses interface


// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "DanceCharacter.h"
#include "DanceInterface.h"
#include "DanceBot.generated.h"

/**
 * 
 */
UCLASS()
class ADanceBot : public ADanceCharacter, public IDanceInterface
{
	GENERATED_UCLASS_BODY()

public:
	virtual FString ToString() OVERRIDE;
	
};

cpp:


// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "Dance.h"
#include "DanceBot.h"


ADanceBot::ADanceBot(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

}


//IToStringInterface
FString ADanceBot::ToString()
{
	return "DanceBot!";
}

Ok It compiles! But still this generated thing is confusing. Some basic classes can be generated from editor but can’t do the same for interface… ?

By now I never used the built in class creation wizard.
I think it provides only templates for the few most used classes.

No it provides templates for every class in your project. There is a checkbox “show all classes”.

Good to know, thanks!