UE4中‘Blueprintable’如何使类可蓝图化?其工作原理疑问
Blueprintable Specifier: Your Two Questions Answered Great question—let’s break this down clearly since Blueprintable is such a foundational tool for bridging C++ and Blueprints in UE4.
1. How does Blueprintable enable a class to be "blueprintable"?
Put simply, Blueprintable acts as a permission slip that lets your C++ class be used as a base class for Blueprint classes in the Unreal Editor. Without this specifier, your C++ class is invisible to the Blueprint creation system—you can’t select it as a parent when making a new Blueprint.
Once you tag a class with Blueprintable:
- It shows up in the "Pick Parent Class" menu when you create a new Blueprint, letting you spawn Blueprint subclasses that inherit all its C++ logic.
- Any members (functions, properties, events) in the C++ class marked with complementary specifiers like
BlueprintCallable,BlueprintReadOnly, orBlueprintImplementableEventbecome accessible and editable in the Blueprint editor. - You can extend the class with visual scripting logic—adding event graphs, modifying properties at design time, or overriding C++ functions with Blueprint implementations (if you used
BlueprintNativeEvent).
For example:
UCLASS(Blueprintable) class MYGAME_API UMyCustomComponent : public UActorComponent { GENERATED_BODY() public: // This function will be callable from Blueprints UFUNCTION(BlueprintCallable, Category = "My Component") void DoSomethingCool(); // This property will be editable in the Blueprint details panel UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Component") float HealthValue; };
With this setup, you can create a Blueprint subclass of UMyCustomComponent and tweak HealthValue or call DoSomethingCool() directly in the Blueprint graph.
2. What’s the underlying mechanism of the Blueprintable specifier?
The magic here ties into UE4’s Unreal Header Tool (UHT) and reflection system—these are the backend systems that connect C++ code to the Unreal Editor and runtime.
Here’s a step-by-step breakdown of how it works:
- When you add
UCLASS(Blueprintable)to your class, the UHT scans your header file during compilation and generates additional reflection metadata for the class. - This metadata registers the class with UE4’s internal class registry, flagging it as eligible to be a Blueprint parent. The editor reads this registry to populate the parent class selection menu.
- The specifier also enables blueprint compatibility checks: it ensures that the class’s structure (inheritance, member accessibility) aligns with what UE’s Blueprint system expects, so the editor can safely generate a Blueprint subclass wrapper around it.
- At runtime, when a Blueprint instance is spawned, UE4 uses this reflection data to initialize both the C++ base class and the Blueprint-specific logic, ensuring they work in sync.
A quick side note: Don’t confuse Blueprintable with BlueprintType—BlueprintType lets you use instances of the class as variables inside Blueprints, while Blueprintable lets you create Blueprint subclasses of the class. You can use both together if you need both capabilities.
内容的提问来源于stack exchange,提问作者Paler

