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 class called APostBeginPlayTest.
  5. Modify the class declaration so that it also inherits IPostBeginPlay:
    UCLASS()
    class UE4COOKBOOK_API APostBeginPlayTest : public AActor, public IPostBeginPlay
  6. Compile your project. Inside the editor, drag an instance of APostBeginPlayTest into your level. With the instance selected, click on Open Level Blueprint:
    How to do it...
  7. Inside the Level Blueprint, right-click and Create a Reference to PostBeginPlayTest1.
    How to do it...
  8. 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 available. Click on it to insert a call to your native UInterface implementation from Blueprint.
    How to do it...
  9. Finally, connect the execution pin (white arrow) from the BeginPlay node to the execution pin for OnPostBeginPlay.
    How to do it...
  10. 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.

How it works...

  1. The UINTERFACE/IInterface pair function as in other recipes, with the UInterface containing reflection information and other data, and the IInterface functioning as the actual interface class that can be inherited from.
  2. The most significant element that allows the function inside IInterface to be exposed to Blueprint is the UFUNCTION specifier.
  3. BlueprintCallable marks this function as one that can be called from the Blueprint system.
  4. Any functions exposed to Blueprint in any way require a Category value also. This Category value specifies the heading under which the function will be listed in the context menu.
  5. The function must also be marked virtual—this is so that a class that implements the interface via native code can override the implementations of the functions inside it. Without the virtual specifier, the Unreal Header Tool will give you an error indicating that you have to either add virtual, or BlueprintImplementableEvent as a UFUNCTION specifier.
  6. The reason for this is that without either of those, the interface function wouldn't be overridable in C++ (due to the absence of virtual), or Blueprint (because BlueprintImplementableEvent was missing). An interface that can't be overridden, but only inherited, has limited utility, so Epic have chosen not to support it within UInterfaces.
  7. We then provide a default implementation of the OnPostBeginPlay function, which uses the GEngine pointer to display a debug message confirming that the function was invoked.

See also

  • Refer to Chapter 8, Integrating C++ and the Unreal Editor, for a number of recipes showing how you can integrate your C++ classes with Blueprint
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.113.111