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.
Wearable
(IWearable
, UWearable
).UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Wearable) int32GetStrengthRequirement(); UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Wearable) boolCanEquip(APawn* Wearer); UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Wearable) voidOnEquip(APawn* Wearer);
int32 IWearable::GetStrengthRequirement_Implementation() { return 0; } Bool IWearable::CanEquip_Implementation(APawn* Wearer) { return true; } Void IWearable::OnEquip_Implementation(APawn* Wearer) { }
Actor
class called Boots
inside the editor.#include "Wearable.h"
to the header file for Boots
.UCLASS() class UE4COOKBOOK_API ABoots : public AActor, public IWearable
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(); }
Gloves
based on Actor
.Wearable
as the interface that the Gloves
actor will implement.Gloves
, override the OnEquip
function like this:Gloves
and Boots
into your level for testing purposes.Boots
performs the default behavior, but Gloves
performs the blueprint-defined behavior.UFUNCTION
specifiers together: BlueprintNativeEvent
and BlueprintCallable
.BlueprintCallable
has been shown in previous recipes, and is a way of marking your UFUNCTION
as visible and invokable in the Blueprint Editor.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
._Implementation
to the end.GetStrengthRequirement
, but has no implementation, because that is autogenerated.GetStrengthRequirement_Implementation
, but there is no declaration for it, because it is also autogenerated.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.Gloves
also implements IWearable
, but has an overridden implementation for OnEquip
defined in Blueprint.OnEquip
for the two actors.3.22.51.241