Interface
Official Documentation: docs.unrealengine.com.
Create Interface
First, Create a new C++ file in the Unreal Engine Editor
and select Unreal Interface
as the Parent Class. Also, if you want to trigger it OnOverlap
, dont forget to enable OverlapAllDynamics
in your Details Panel
for the Actors that Interact with eachother.
tip
Interface is great to interact between Actors
YourInterface.h
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "YourInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UYourInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class YOURGAME_API IYourInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
// for this example were creating a Interact function
virtual void Interact() = 0; // = 0; so we signal, to the compiler that there is no implementation of this function in the YourInterface.cpp
};
Setup the Actor you want to Interact with, in this case SomeActor
SomeActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "YourInterface.h" // include your Interface file
#include "SomeActor.generated.h"
UCLASS()
// extend your class with your Interface
class YOURGAME_API ASomeActor : public AActor, public IYourInterface // add Interface here
{
GENERATED_BODY()
public:
ASomeActor();
// Our Interact function from YourInterface.h
virtual void Interact() override;
...
}
SomeActor.cpp
#include "SomeActor.h"
ASomeActor::ASomeActor()
{
//
}
// Called when the game starts or when spawned
void ASomeActor::BeginPlay()
{
Super::BeginPlay();
}
void ASomeActor::Interact()
{
// define what SomeActor should do on Interact
// in this case we just print a log to the console
UE_LOG(LogTemp, Warning, TEXT("You Successfully Interacted with SomeActor"));
}
For this example we'll call the interface from YourCharacter Class
YourCharacter.h
#include "YourInterface.h" // add the Interface file
YourCharacter.cpp
#include "YourCharacter.h
AYourCharacter::AYourCharacter()
{
//
}
// Called when the game starts or when spawned
void AYourCharacter::BeginPlay()
{
Super::BeginPlay();
}
// For demonstration purposes: We'll just cast a LineTrace every Tick
// You obviously can set up your Inputs then cast a LineTrace from there
void AYourCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
// LineTrace Parameters
FHitResult Hit;
FVector Forward = FollowCamera->GetForwardVector() * 1000.f;
FVector Start = GetCapsuleComponent()->GetComponentLocation();
FVector End = Start + Forward;
FCollisionQueryParams CollisionParams;
CollisionParams.AddIgnoredActor(this);
if (Controller != nullptr) // PlayerController is not null
// add the parameters to the LineTrace
GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, CollisionParams);
// Visualize the LineTrace with DrawDebugLine
DrawDebugLine(GetWorld(), Start, End, Hit.bBlockingHit ? FColor::Blue : FColor::Red, false, 5.0f, 1.0f, 10.0f);
if (Hit.bBlockingHit && IsValid(Hit.GetActor())) // check if a Actor got got
{
// Cast YourInterface and check if its not null
IYourInterface* Interface = Cast<IYourInterface>(Hit.GetActor());
if (Interface)
{
// if Interface is not null call the Interact function
Interface->Interact();
// This prints the following in the console:
// "You Successfully Interacted with SomeActor" from SomeActor
}
}
else
{
// If the LineTrace does not hit an Actor
UE_LOG(LogTemp, Log, TEXT("No Actor got got"));
}
}
This was an example on how to use Interfaces in Unreal Engine C++.