Creating a UInterface

UInterfaces are a pair of classes that work together to enable classes to exhibit polymorphic behavior among multiple class hierarchies. This recipe shows you the basic steps involved in creating a UInterface purely in code.

How to do it...

  1. UInterfaces don't show up inside the main class wizard within Unreal, so we'll need to add the class manually using Visual Studio.
  2. Right click on your Source folder inside Solution Explorer, and select Add | New Item.
  3. Select a .h file to start, and name it MyInterface.h.
  4. Make sure you change the directory for the item to be placed in from Intermediate to Source/ProjectName.
  5. Click on OK to create a new header file in your project folder.
  6. Repeat the steps in order to create MyInterface.cpp as your implementation file.
  7. Add the following code to the header file:
    #include "MyInterface.generated.h"
    /**  */
    UINTERFACE()
    class UE4COOKBOOK_API UMyInterface: public UInterface
    {
      GENERATED_BODY()
    };
    
    /**  */
    class UE4COOKBOOK_API IMyInterface
    {
      GENERATED_BODY()
    
      public:
      virtualFStringGetTestName();
    };
  8. Implement the class with this code in the .cpp file:
    #include "UE4Cookbook.h"
    #include "MyInterface.h"
    
    FString IMyInterface::GetTestName()
    {
      unimplemented();
      return FString();
    }
  9. Compile your project to verify that the code was written without errors.

How it works...

  1. UInterfaces are implemented as a pair of classes declared in the interface's header.
  2. As always, because we are leveraging Unreal's reflection system, we need to include our generated header file. Refer to Handling events implemented via virtual functions in Chapter 5, Handling Events and Delegates, for more information.
  3. As with classes that inherit from UObject, which uses UCLASS, we need to use the UINTERFACE macro to declare our new UInterface.
  4. The class is tagged UE4COOKBOOK_API to help with the exporting of library symbols.
  5. The base class for the UObject portion of the interface is UInterface.
  6. Just like UCLASS types, we require a macro to be placed inside the body of our class so that the auto-generated code is inserted into it.
  7. That macro is GENERATED_BODY() for UInterfaces. The macro must be placed at the very start of the class body.
  8. The second class is also tagged UE4COOKBOOK_API, and is named in a specific way.
  9. Note that the UInterface-derived class and the standard class have the same name but a different prefix. The UInterface-derived class has the prefix U, and the standard class has the prefix I.
  10. This is important as this is how the Unreal Header Tool expects the classes to be named for the code it generates to work properly.
  11. The plain native Interface class requires its own autogenerated content, which we include using the GENERATED_BODY() macro.
  12. We declare functions that classes inheriting the interface should implement inside IInterface.
  13. Within the implementation file, we implement the constructor for our UInterface, as it is declared by the Unreal Header Tool, and requires an implementation.
  14. We also create a default implementation for our GetTestName() function. Without this, the linking phase of compilation will fail. This default implementation uses the unimplemented() macro, which will issue a debug assert when the line of code is executed.

See also

  • Refer to Passing payload data with a delegate binding in Chapter 5, Handling Events and Delegates; the first recipe, in particular, explains some of the principles that we've applied here
..................Content has been hidden....................

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