Implementing a UInterface on an object

Ensure that you've followed the previous recipe in order to have a UInterface ready to be implemented.

How to do it...

  1. Create a new Actor class using the Unreal Wizard, called SingleInterfaceActor.
  2. Add IInterface—in this case, IMyInterface—to the public inheritance list for our new Actor class:
    class UE4COOKBOOK_API ASingleInterfaceActor : public AActor, public IMyInterface
  3. Add an override declaration to the class for the IInterface function(s) that we wish to override:
    FStringGetTestName() override;
  4. Implement the overridden function in the implementation file by adding the following code:
    FStringASingleInterfaceActor::GetTestName()
    {
      return IMyInterface::GetTestName();
    }

How it works...

  1. C++ uses multiple inheritance ...

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.