Exposing UInterface methods to Blueprint from a native base class

Being able to define UInterface methods in C++ is great, but they should be accessible from Blueprint too. Otherwise, designers or others who are using Blueprint won't be able to interact with your UInterface. This recipe shows you how to make a function from an interface callable within the Blueprint system.

How to do it...

  1. Create a UInterface called UPostBeginPlay/IPostBeginPlay.
  2. Add the following virtual method to IPostBeginPlay:
    UFUNCTION(BlueprintCallable, Category=Test)
    virtual void OnPostBeginPlay();
  3. Provide an implementation of the function:
    voidIPostBeginPlay::OnPostBeginPlay()
    {
      GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, "PostBeginPlay called");
    }
  4. Create a new Actor ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.