How to do it...

  1. Create a UInterface called PostBeginPlay:

  1. Open PostBeginPlay.h in Visual Studio and update the UINTERFACE of UPostBeginPlay and add the following virtual method in IPostBeginPlay:
#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "PostBeginPlay.generated.h"

UINTERFACE(meta = (CannotImplementInterfaceInBlueprint))
class UPostBeginPlay : public UInterface
{
GENERATED_BODY()
};

/**
*
*/
class CHAPTER_08_API IPostBeginPlay
{
GENERATED_BODY()

// Add interface functions to this class. This is the
// class that will be inherited to implement
// this interface.
public:
UFUNCTION(BlueprintCallable, Category = Test)
virtual void OnPostBeginPlay();
};
  1. Provide an implementation of the function:
#include "PostBeginPlay.h"

// Add default functionality here for any IPostBeginPlay
// functions that are not pure virtual.
void IPostBeginPlay::OnPostBeginPlay()
{
GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, "PostBeginPlay called");
}
  1. Create a new Actor class called APostBeginPlayTest:

  1. Modify the class declaration so that it also inherits IPostBeginPlay:
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PostBeginPlay.h"
#include "PostBeginPlayTest.generated.h"

UCLASS()
class CHAPTER_08_API APostBeginPlayTest : public AActor, public IPostBeginPlay
  1. Compile your project. Inside the editor, drag an instance of APostBeginPlayTest into your level:

  1. With the instance selected in the World Outliner, click on Blueprints | Open Level Blueprint:

  1. Inside the Level Blueprint, right-click and Create a Reference to PostBeginPlayTest1:
Note that you can also use the drag-and-drop method we discussed in the Inheriting UInterfaces from one another recipe from the previous chapter.
  1. Drag away from the blue pin on the right-hand side of your actor reference, then search the context menu for onpost to see your new interface function. Click on it to insert a call to your native UInterface implementation from Blueprint:

  1. Finally, connect the execution pin (white arrow) from the BeginPlay node to the execution pin for OnPostBeginPlay:

  1. When you play your level, you should see the message PostBeginPlay called visible on screen for a short amount of time, verifying that Blueprint has successfully accessed and called through to your native code implementation of the UInterface.