Being able to define UInterface
methods in C++ is great, but they should be accessible from Blueprint too. Otherwise, designers or others who are using Blueprint won't be able to interact with your UInterface
. This recipe shows you how to make a function from an interface callable within the Blueprint system.
UInterface
called UPostBeginPlay
/IPostBeginPlay
.virtual
method to IPostBeginPlay
:UFUNCTION(BlueprintCallable, Category=Test) virtual void OnPostBeginPlay();
voidIPostBeginPlay::OnPostBeginPlay() { GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, "PostBeginPlay called"); }
Actor
class called APostBeginPlayTest
.IPostBeginPlay
:UCLASS() class UE4COOKBOOK_API APostBeginPlayTest : public AActor, public IPostBeginPlay
APostBeginPlayTest
into your level. With the instance selected, click on Open Level Blueprint:onpost
to see your new interface function available. Click on it to insert a call to your native UInterface
implementation from Blueprint.BeginPlay
node to the execution pin for OnPostBeginPlay
.UInterface
.UINTERFACE
/IInterface
pair function as in other recipes, with the UInterface
containing reflection information and other data, and the IInterface
functioning as the actual interface class that can be inherited from.IInterface
to be exposed to Blueprint is the UFUNCTION
specifier.BlueprintCallable
marks this function as one that can be called from the Blueprint system.Category
value also. This Category
value specifies the heading under which the function will be listed in the context menu.virtual
—this is so that a class that implements the interface via native code can override the implementations of the functions inside it. Without the virtual
specifier, the Unreal Header Tool will give you an error indicating that you have to either add virtual
, or BlueprintImplementableEvent
as a UFUNCTION
specifier.virtual
), or Blueprint (because BlueprintImplementableEvent
was missing). An interface that can't be overridden, but only inherited, has limited utility, so Epic have chosen not to support it within UInterfaces.OnPostBeginPlay
function, which uses the GEngine
pointer to display a debug message confirming that the function was invoked.3.148.113.111