1. Create interface class

    image.png

  2. 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();
    };
    
  3. Add the public IMyInterface extension

  4. Add the #include "MyInterface.h" include

  5. 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;
    };
    
    
  6. 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);
    
    }
    
  7. Check if interface is implemented in other Object if(AnotherActorClass->Implements<UMyInterface>())

  8. Check if other Object is valid if(AnotherActorClass != nullptr)

  9. 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);
    
    }
    
  10. If you make your classes Blueprintable the interface will be under Inherited Interfaces

    BP_AnotherActor

    BP_AnotherActor