- Create a new interface called Wearable ( Creating both IWearable & UWearable):
- Add the following functions to the header of the IWearable class:
class CHAPTER_08_API IWearable
{
GENERATED_BODY()
// Add interface functions to this class. This is the
// class that will be inherited to implement
// this interface.
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable,
Category = Wearable)
int32 GetStrengthRequirement();
UFUNCTION(BlueprintNativeEvent, BlueprintCallable,
Category = Wearable)
bool CanEquip(APawn* Wearer);
UFUNCTION(BlueprintNativeEvent, BlueprintCallable,
Category = Wearable)
void OnEquip(APawn* Wearer);
};
UE 4.20 and above does not allow us to create a default implementation for a function if it is defined in an interface class, so we will have to use UE's default empty implementation, which gives us the default value as a return for each function. This is because, in C# and other languages that have interfaces, they are not supposed to have a default implementation.
- Create a new Actor class called Boots inside the editor:
- Add #include "Wearable.h" to the header file for Boots and modify the class declaration, as follows:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Wearable.h"
#include "Boots.generated.h"
UCLASS()
class CHAPTER_08_API ABoots : public AActor, public IWearable
- Add the following implementation of the pure virtual functions that were created by our Interface:
UCLASS()
class CHAPTER_08_API ABoots : public AActor, public IWearable
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABoots();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Implementing the functions needed for IWearable
virtual void OnEquip_Implementation(APawn* Wearer) override
{
GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red,
"Item being worn");
}
virtual bool CanEquip_Implementation(APawn* Wearer) override
{
return true;
}
virtual int32 GetStrengthRequirement_Implementation() override
{
return 0;
}
};
If you do not know how to do the following two steps, check out the previous recipe, Implementing UInterface functions in Blueprint.
- Compile your script so that we will have access to the new functions we have created.
- Create a new Blueprint class called Gloves based on Actor by going to the Content Browser, opening the Content folder, and then right clicking and then selecting Blueprint Class.
- In the Class Settings menu, under the Details tab, scroll down to the Implemented Interfaces property and click on the Add button and select Wearable as the interface that the Gloves actor will implement:
Adding the Wearable interface
- Afterwards, hit the Compile button in order to apply the change.
- Open the Event Graph and right-click it to create a new event. From the search bar, type in on equip, and you should see our event under the Add Event section:
- This allows us to override the OnEquip function from the default implementation to do whatever we want. For instance, add a Print String node with the In String being set to Gloves being worn:
- Click on the Compile button and then you can close the Blueprint. Drag a copy of both Gloves and Boots into your level for testing purposes.
- Once added, add the following blueprint code to your level:
- Verify that Boots performs the default behavior, but that Gloves performs the blueprint-defined behavior: