Unreal Engine C++ Interfaces
Implementing Simple Interface
Interface : We need sometime common functionality in our game-play classes so we define here some functionality or other working objects and implement it a require game-play classes.
In C++ we use multiple inheritance to solve this.
Unreal Engine provide Interface more power of our game-play classes with Blueprint accessible macros;
Require :
Visual Studio 2019
Unreal Engine 4.22
C++ Project
Now you can see This type of code
.h file
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MyFirstInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyFirstInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class COVERTRACK_API IMyFirstInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual FString GetName();
};
In .cpp
#include "MyFirstInterface.h"
// Add default functionality here for any IMyFirstInterface functions that are not pure virtual.
FString IMyFirstInterface::GetName() {
unimplemented();
return FString();
}
Compile!!
Make a Actor Class
already have or if not Make it from New C++ class > Actor
youractor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyFirstInterface.h"
#include "ES_SpawanEnemy.generated.h"
UCLASS()
class COVERTRACK_API AES_SpawanEnemy : public AActor, public IMyFirstInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AES_SpawanEnemy();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
.h file
public:
FString GetName() override ;
.cpp
FString AES_SpawanEnemy::GetName()
{
return IMyFirstInterface::GetName();
}
compile!!
Checking Interface working or not
Open GameMode Class
#include "CoverTrackGameModeBase.h"
#include "ES_SpawanEnemy.h"
#include "MyFirstInterface.h"
#include "Engine/Engine.h"
void ACoverTrackGameModeBase::BeginPlay() {
Super::BeginPlay();
FTransform SpawanLocation;
AES_SpawanEnemy* SpawanActor = GetWorld()->SpawnActor<AES_SpawanEnemy>
(AES_SpawanEnemy::StaticClass(), SpawanLocation);
UClass* ActorClass = SpawanActor->GetClass();
if (ActorClass->ImplementsInterface(UMyFirstInterface::StaticClass()))
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("Interface Working"));
}
}
}
Introduction
In C++ we use multiple inheritance to solve this.
Unreal Engine provide Interface more power of our game-play classes with Blueprint accessible macros;
Require :
Visual Studio 2019
Unreal Engine 4.22
C++ Project
Creating a UInterface
Add New > New C++ Class > Scroll Down Class >Unreal UInterface > Next > name it!!Now you can see This type of code
.h file
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MyFirstInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyFirstInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class COVERTRACK_API IMyFirstInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual FString GetName();
};
In .cpp
#include "MyFirstInterface.h"
// Add default functionality here for any IMyFirstInterface functions that are not pure virtual.
FString IMyFirstInterface::GetName() {
unimplemented();
return FString();
}
Compile!!
Implement UInterface
Now we implement the function we define in Interface and implement in game-play class;Make a Actor Class
already have or if not Make it from New C++ class > Actor
youractor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyFirstInterface.h"
#include "ES_SpawanEnemy.generated.h"
UCLASS()
class COVERTRACK_API AES_SpawanEnemy : public AActor, public IMyFirstInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AES_SpawanEnemy();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
.h file
public:
FString GetName() override ;
.cpp
FString AES_SpawanEnemy::GetName()
{
return IMyFirstInterface::GetName();
}
compile!!
Checking Interface working or not
Open GameMode Class
#include "CoverTrackGameModeBase.h"
#include "ES_SpawanEnemy.h"
#include "MyFirstInterface.h"
#include "Engine/Engine.h"
void ACoverTrackGameModeBase::BeginPlay() {
Super::BeginPlay();
FTransform SpawanLocation;
AES_SpawanEnemy* SpawanActor = GetWorld()->SpawnActor<AES_SpawanEnemy>
(AES_SpawanEnemy::StaticClass(), SpawanLocation);
UClass* ActorClass = SpawanActor->GetClass();
if (ActorClass->ImplementsInterface(UMyFirstInterface::StaticClass()))
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("Interface Working"));
}
}
}
Comments
Post a Comment