Create interface class

Add your own functions to the interface
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MyInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(Blueprintable)
class UMyInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class IMyInterface
{
GENERATED_BODY()
public:
//Add your own functions here
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
void MyFunc();
};
Add the public IMyInterface extension
Add the #include "MyInterface.h" include
Override the interface functions void MyFunc_Implementation() override;
#pragma once
#include "CoreMinimal.h"
#include "MyInterface.h"
#include "GameFramework/Actor.h"
#include "AnotherActor.generated.h"
UCLASS(Blueprintable)
class AAnotherActor : public AActor, public IMyInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AAnotherActor();
//Override function here
void MyFunc_Implementation() override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Define the overriden interface function
#include "AnotherActor.h"
// Sets default values
AAnotherActor::AAnotherActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
void AAnotherActor::MyFunc_Implementation()
{
//Custom C++ implementation here
UE_LOG(LogTemp, Warning, TEXT("MyFunc called!"))
}
// Called when the game starts or when spawned
void AAnotherActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AAnotherActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Check if interface is implemented in other Object
if(AnotherActorClass->Implements<UMyInterface>())
Check if other Object is valid if(AnotherActorClass != nullptr)
Call the interface function IMyInterface::Execute_MyFunc(AnotherActorClass);
#include "TestActor.h"
#include "AnotherActor.h"
#include "MyInterface.h"
// Sets default values
ATestActor::ATestActor(): AnotherActorClass(nullptr)
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ATestActor::BeginPlay()
{
Super::BeginPlay();
//Check if implemented and call interface
if(AnotherActorClass->Implements<UMyInterface>() && AnotherActorClass != nullptr)
IMyInterface::Execute_MyFunc(AnotherActorClass);
}
// Called every frame
void ATestActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
If you make your classes Blueprintable the interface will be under Inherited Interfaces

BP_AnotherActor