Exposing C++ to Blueprints
Blueprints are Unreal's visual scripting language, and every Unreal team uses them, C++-heavy teams included. The usual architecture is C++ base classes with Blueprint subclasses: C++ defines the systems, data and performance-sensitive logic; Blueprints choose assets, tune values and script one-off behaviour. This lesson covers the bridge.
Calling C++ from Blueprints
UCLASS(Blueprintable)
class ARENA_API AArenaDoor : public AActor
{
GENERATED_BODY()
public:
// A node with exec pins. Designers call it from level scripts, triggers, UI.
UFUNCTION(BlueprintCallable, Category = "Door")
void Open(AActor* Opener);
// A pure node: no exec pins, re-evaluated wherever its output is used. Must not change state.
UFUNCTION(BlueprintPure, Category = "Door")
bool IsOpen() const { return bIsOpen; }
// Several outputs: non-const reference parameters become output pins.
UFUNCTION(BlueprintCallable, Category = "Door")
bool TryUnlock(int32 KeyId, FText& OutFailReason);
// Static helpers usually live in a UBlueprintFunctionLibrary (below).
protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Door")
bool bIsOpen = false;
};Function libraries hold static helpers that appear as global nodes:
UCLASS()
class ARENA_API UArenaStatics : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, Category = "Arena", meta = (WorldContext = "WorldContextObject"))
static int32 GetAliveEnemyCount(const UObject* WorldContextObject);
};meta = (WorldContext = "WorldContextObject") lets the node find the world automatically, so the parameter is hidden in Blueprint graphs.
Calling Blueprints from C++: events
Two ways to let a Blueprint provide behaviour that C++ triggers:
// 1. BlueprintImplementableEvent: C++ declares, Blueprint implements. No C++ body.
UFUNCTION(BlueprintImplementableEvent, Category = "Door")
void OnOpened(); // play a sound, a timeline, a camera shake in BP
// 2. BlueprintNativeEvent: C++ provides a default, Blueprint may override it.
UFUNCTION(BlueprintNativeEvent, Category = "Door")
bool CanOpen(AActor* Opener) const;// The C++ default goes in the _Implementation function:
bool AArenaDoor::CanOpen_Implementation(AActor* Opener) const
{
return IsValid(Opener) && !bIsLocked;
}
void AArenaDoor::Open(AActor* Opener)
{
if (bIsOpen || !CanOpen(Opener)) return; // call CanOpen, not CanOpen_Implementation
bIsOpen = true;
OnOpened(); // runs the Blueprint graph, if any
}Always call the plain name (CanOpen): the generated thunk dispatches to the Blueprint override if there is one, otherwise to _Implementation. A Blueprint override can call the C++ version with Add Call to Parent Function.
Interfaces
A UINTERFACE lets unrelated classes share a contract that both C++ and Blueprints can implement, for example "interactable":
UINTERFACE(MinimalAPI, Blueprintable)
class UArenaInteractable : public UInterface { GENERATED_BODY() };
class ARENA_API IArenaInteractable
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interact")
void Interact(APawn* Instigator);
};
// Calling it (works whether C++ or a Blueprint implements it):
if (Target->Implements<UArenaInteractable>())
{
IArenaInteractable::Execute_Interact(Target, GetPawn());
}Interfaces with BlueprintNativeEvent/BlueprintImplementableEvent functions must be called through the static Execute_ wrapper. A plain Cast<IArenaInteractable>(Target) fails for Blueprint-only implementations.
A model of the native-event dispatch
UHT generates a thunk for CanOpen that checks whether the object's class has a Blueprint override and calls it, falling back to _Implementation. Here is that dispatch in plain C++, with a "Blueprint graph" as a stored function.
#include <functional>
#include <iostream>
#include <string>
struct Actor { std::string Name; bool HasKey; };
struct DoorClass { // stands in for a UClass: C++ or Blueprint subclass
std::string Name;
std::function<bool(const Actor&, std::function<bool(const Actor&)>)> CanOpenOverride; // the BP graph
std::function<void()> OnOpenedGraph; // BlueprintImplementableEvent
};
struct Door {
const DoorClass* Class;
bool bIsLocked = true, bIsOpen = false;
bool CanOpen_Implementation(const Actor& who) const { return !bIsLocked || who.HasKey; }
bool CanOpen(const Actor& who) const { // the generated thunk
if (Class->CanOpenOverride)
return Class->CanOpenOverride(who, [this](const Actor& a) { return CanOpen_Implementation(a); });
return CanOpen_Implementation(who);
}
void OnOpened() const { if (Class->OnOpenedGraph) Class->OnOpenedGraph(); } // no graph: no-op
void Open(const Actor& who) {
std::cout << Class->Name << " <- " << who.Name << ": ";
if (bIsOpen || !CanOpen(who)) { std::cout << "stays shut\n"; return; }
bIsOpen = true;
std::cout << "opens";
OnOpened();
std::cout << "\n";
}
};
int main() {
DoorClass cppDoor{"AArenaDoor", nullptr, nullptr};
DoorClass bossDoor{"BP_BossDoor",
[](const Actor& who, auto parent) { return parent(who) && who.Name == "Hero"; }, // calls the parent
[] { std::cout << " + camera shake + boss music"; }};
Actor hero{"Hero", true}, grunt{"Grunt", true}, thief{"Thief", false};
Door{&cppDoor}.Open(grunt);
Door{&cppDoor}.Open(thief);
Door{&bossDoor}.Open(grunt);
Door{&bossDoor}.Open(hero);
}AArenaDoor <- Grunt: opens
AArenaDoor <- Thief: stays shut
BP_BossDoor <- Grunt: stays shut
BP_BossDoor <- Hero: opens + camera shake + boss music
The C++ door uses only the native rule; BP_BossDoor adds a condition on top of it ("Add Call to Parent Function") and implements the OnOpened event.
Test yourself
Where does the C++ body of a BlueprintNativeEvent named CanOpen go?
- CanOpen
- CanOpen_Implementation
- CanOpen_Validate
- ReceiveCanOpen
How do you call an interface function that a Blueprint may implement?
- Cast<IArenaInteractable>(Target)->Interact()
- IArenaInteractable::Execute_Interact(Target, Pawn)
- Target->Interact()
- CallFunctionByNameWithArguments
What is special about a BlueprintPure node?
- It runs on the server
- It has no exec pins and should not change state
- It can only return bool
- It is cached forever