Creating C++ UInterface function implementations that can be overridden in Blueprint

Just as with the previous recipe, UInterfaces are useful, but that utility is severely limited without their functionality being usable by designers.

The previous recipe shows you how to call C++ UInterface functions from Blueprint; this recipe will show you how to replace the implementation of a UInterface function with your own custom Blueprint-only function.

How to do it...

  1. Create a new interface called Wearable (IWearable, UWearable).
  2. Add the following functions to the header:
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Wearable)
    int32GetStrengthRequirement();
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Wearable)
    boolCanEquip(APawn* Wearer);
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Wearable)
    voidOnEquip(APawn* Wearer);
  3. Add the following function implementations in the implementation file:
    int32 IWearable::GetStrengthRequirement_Implementation()
    {
      return 0;
    }
    
    Bool IWearable::CanEquip_Implementation(APawn* Wearer)
    {
      return true;
    }
    
    Void IWearable::OnEquip_Implementation(APawn* Wearer)
    {
    
    }
  4. Create a new Actor class called Boots inside the editor.
  5. Add #include "Wearable.h" to the header file for Boots.
  6. Modify the class declaration as follows:
    UCLASS()
    class UE4COOKBOOK_API ABoots : public AActor, public IWearable
  7. Add the following implementation of the pure virtual functions created by our Interface:
    virtual void OnEquip_Implementation(APawn* Wearer) override
    {
      IWearable::OnEquip_Implementation(Wearer);
    }
    virtual bool CanEquip_Implementation(APawn* Wearer) override
    {
      return IWearable::CanEquip_Implementation(Wearer);
    }
    virtual int32 GetStrengthRequirement_Implementation() override
    {
      return IWearable::GetStrengthRequirement_Implementation();
    }
  8. Create a new Blueprint class called Gloves based on Actor.
  9. In the class settings, select Wearable as the interface that the Gloves actor will implement.
  10. Within Gloves, override the OnEquip function like this:
    How to do it...
  11. Drag a copy of both Gloves and Boots into your level for testing purposes.
  12. Add the following blueprint code to your level:
    How to do it...
  13. Verify that Boots performs the default behavior, but Gloves performs the blueprint-defined behavior.
    How to do it...

How it works...

  1. This recipe uses two UFUNCTION specifiers together: BlueprintNativeEvent and BlueprintCallable.
  2. BlueprintCallable has been shown in previous recipes, and is a way of marking your UFUNCTION as visible and invokable in the Blueprint Editor.
  3. BlueprintNativeEvent signifies a UFUNCTION that has a default C++ (native code) implementation, but is also overridable in Blueprint. It's the combination of a virtual function along with BlueprintImplementableEvent.
  4. In order for this mechanism to work, the Unreal Header Tool generates the body of your functions so that the Blueprint version of the function is called if it exists; otherwise, it dispatches the method call through to the native implementation.
  5. In order to separate your default implementation from the dispatch functionality though, UHT defines a new function that takes its name from your declared function, but appends _Implementation to the end.
  6. This is why the header file declares GetStrengthRequirement, but has no implementation, because that is autogenerated.
  7. It is also why your implementation file defines GetStrengthRequirement_Implementation, but there is no declaration for it, because it is also autogenerated.
  8. The Boots class implements IWearable, but doesn't override the default functionality. However, because the _Implementation functions are defined as virtual, we still need to explicitly implement the interface functions, and then call the default implementation directly.
  9. In contrast, Gloves also implements IWearable, but has an overridden implementation for OnEquip defined in Blueprint.
  10. This can be verified when we use Level Blueprints to call OnEquip for the two actors.
..................Content has been hidden....................

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