C++ Interfaces Accessible In blueprints
This page uses most of its knowledge from https://dev.epicgames.com/documentation/en-us/unreal-engine/interfaces-in-unreal-engine
To create a c++ interface, add a new c++ class and under common classes select Unreal Interface
This generates two classes U<ClassName> and I<ClassName>
You put your functions in the I<ClassName>
Unlike Traditional c++ interfaces, you don't use virtual functions
and the members don't need to be implemented. While you can force this constraint
that would prevent you from using the interfaces in blueprints
From epics website
Functions with the BlueprintImplementableEvent specifier can not be overridden in C++, but can be overridden in any Blueprint class that implements or inherits your interface. The following is an example of a C++ interface declaration for a BlueprintImplementableEvent:
In the example below we added Use to the appropriate class
<syntaxhighlight lang="cpp">
- include "CoreMinimal.h"
- include "UObject/Interface.h"
- include "CI_Item.generated.h"
// This class does not need to be modified. UINTERFACE(MinimalAPI) class UCI_Item : public UInterface { GENERATED_BODY() };
/**
* */
class ININO0_API ICI_Item { GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface. public: UFUNCTION(BlueprintCallable, BlueprintImplementableEvent) bool Use(); }; </syntaxhighlight>
Implementing the Interface
All you need to do is include the header file for your interface in the header file where you want to use the interface
and Inherit from the interface
<syntaxhighlight lang="cpp">
- pragma once
- include "CoreMinimal.h"
- include "GameFramework/Actor.h"
- include "CI_Item.h"
- include "In0_Item.generated.h"
UCLASS() class ININO0_API AIn0_Item : public AActor , public ICI_Item { GENERATED_BODY() public: // Sets default values for this actor's properties AIn0_Item(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Details") USkeletalMeshComponent *SkeletalMesh; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Details") bool Use(); }; </syntaxhighlight>
Override a Blueprint Native Event in C++
To implement a BlueprintNativeEvent in C++, create an additional function with the same name as the BlueprintNativeEvent with an additional _Implementation suffix appended to the name. The following is an example of what this looks like from the ATrap example:
<syntaxhighlight lang="cpp">
- include "CoreMinimal.h"
- include "GameFramework/Actor.h"
- include "ReactToTriggerInterface.h"
- include "Trap.generated.h"
UCLASS(Blueprintable, Category="MyGame") class ATrap : public AActor, public IReactToTriggerInterface { GENERATED_BODY()
public: virtual bool ReactToTrigger() override;
// Blueprint Native Event override bool ReactToTrigger_Implementation() override; }; </syntaxhighlight>