Expose an interface to blueprint

From Epic Wiki
Jump to: navigation, search
BlueprintInterface.png

Template:Rating


NOTE: This tutorial is largely out of date, recommend reading this instead https://wiki.unrealengine.com/Interfaces_in_C%2B%2B#The_Interface


We will create a simple interface that you can use in your blueprints. As you can see in the picture above we will create a simple event called On Interact which exposes your interface "clicked" with argument type ITargetInterface. Note that this argument is only needed in this specific example. If you make your own class with your own event, you can make events with an optional number of arguments. With this interface in place we will be able to call it's functions like GetHealth.

Header file:

<syntaxhighlight lang="Cpp">

  1. pragma once
  2. include "TargetInterface.generated.h"

UINTERFACE(MinimalAPI) class UTargetInterface : public UInterface { GENERATED_UINTERFACE_BODY() };

class ITargetInterface{ GENERATED_IINTERFACE_BODY() public: UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact")) void OnInteract(const TScriptInterface<ITargetInterface> &clicked); virtual float GetHealth();

}; </syntaxhighlight>

.cpp file:

<syntaxhighlight lang="Cpp">

  1. include "YourProject.h"
  2. include "TargetInterface.h"

UTargetInterface::UTargetInterface(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP){

} // Give GetHealth a default implementation float ITargetInterface::GetHealth(){ return 0.0f; } </syntaxhighlight>


The important part here is <syntaxhighlight lang="Cpp"> UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact")) void OnInteract(const TScriptInterface<ITargetInterface> &clicked); </syntaxhighlight>

1.) BlueprintImplementableEvent turns your function into a blueprint event. This event will be called in blueprint when ever you call it in c++.

2.) void OnInteract(const TScriptInterface<ITargetInterface> &clicked); . Sometimes simple events are not enough for example in this case we want to know who has started the OnInteract event.

3.) The most important part is "const TScriptInterface<ITargetInterface> &clicked". We have to wrap our interface inside of an TScriptInterface to expose it to blueprint.

To trigger the event you can from within your C++ code call the function Execute_OnInteract. It always takes the instance of your class as an argument + the optional arguments you defined. In our case this is a TScriptInterface<ITargetInterface> object. Below is the example code, where we have created an instance named actor. We will generate the necessary argument "s" and then call the Execute_OnInteract function.

<syntaxhighlight lang="Cpp"> auto t = InterfaceCast<ITargetInterface>(actor); if (t != nullptr){ TScriptInterface<ITargetInterface> s = TScriptInterface<ITargetInterface>(); s.SetObject(actor);

       s.SetInterface(t);

t->Execute_OnInteract(actor,s); } </syntaxhighlight>

InterfaceCast tries to convert your actor to an ITargetInterface. In this case the variable "t" will be null if the cast has failed. The next step is to wrap our actor who implements our ITargetInterface in an TScriptInterface. You do this by creating an TScriptInterface with the default consturctor and use the SetObject method.

Now the most important part here is that you should not call the method OnInteract because this will result in an runtime error. Instead you need to call the generated utility function which is called Execute_YourFunctionName in this case it is called Execute_OnInteract. The first argument is always the UObject and the following argument are the arguments of your function.

In our case we defined <syntaxhighlight lang="Cpp"> void OnInteract(const TScriptInterface<ITargetInterface> &clicked) </syntaxhighlight>

So the type signature will look like this

<syntaxhighlight lang="Cpp"> Execute_OnInteract(UObject *object, const TScriptInterface<ITargetInterface> &clicked); </syntaxhighlight>


At this point you can already use your event in your blueprints. The problem is that you have no functions that are working with your newly created interface. Let's fix this.

Header code:

<syntaxhighlight lang="Cpp">

  1. pragma once
  1. include "GameFramework/Actor.h"
  2. include "TargetInterface.h"
  3. include "TargetInterfaceBPFunctionLibrary.generated.h"

/**

* 
*/

UCLASS() class UTargetInterfaceBPFunctionLibrary : public UBlueprintFunctionLibrary { GENERATED_UCLASS_BODY()

UFUNCTION(BlueprintCallable, Category="TargetInterface") static float GetHealth(const TScriptInterface<ITargetInterface> &target); }; </syntaxhighlight>

.cpp code:

<syntaxhighlight lang="Cpp">

  1. include "project.h"
  2. include "TargetInterfaceBPFunctionLibrary.h"


UTargetInterfaceBPFunctionLibrary::UTargetInterfaceBPFunctionLibrary(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) {

}

float UTargetInterfaceBPFunctionLibrary::GetHealth(const TScriptInterface<ITargetInterface> &target) { return target->GetHealth(); }

</syntaxhighlight>

That's it. Now you can access your interface inside of your blueprints.

Maikklein