Overriding UInterface functions in C++

One side effect of UInterfaces allowing inheritance in C++ is that we can override default implementations in subclasses as well as in Blueprint. This recipe shows you how to do so.

Getting ready

Follow the recipe Calling native UInterface functions from C++ in which a Physics Cube is created so that you have the class ready.

How to do it...

  1. Create a new Interface called Selectable.
  2. Define the following functions inside ISelectable:
    virtual bool IsSelectable();
    
    virtual bool TrySelect();
    
    virtual void Deselect();
  3. Provide a default implementation for functions like this:
    boolISelectable::IsSelectable() { GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, "Selectable"); return true; } boolISelectable::TrySelect() { ...

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.