Creating Blueprint Callable Functions in C++
In this lesson, you will learn how to bridge the gap between high-performance C++ logic and the flexibility of the Blueprint visual scripting system using the BlueprintCallable specifier.
Why Expose Functions?
While C++ is ideal for complex calculations, memory management, and performance-critical systems, Blueprints are perfect for designers who need to quickly trigger events or tweak gameplay logic. By exposing your C++ functions, you allow designers to call your robust code directly from their Event Graphs, keeping your project organized and accessible.
Implementing the BlueprintCallable Specifier
To make a C++ function accessible in Blueprints, you use the UFUNCTION() macro above your function declaration in the header file. By adding the BlueprintCallable keyword, you tell the Unreal Header Tool (UHT) to generate the necessary code to bridge your function to the editor.
Consider this example where you have a custom health-management system:
// In your Header file (.h)
UFUNCTION(BlueprintCallable, Category = "Combat")
void HealPlayer(float Amount);
By providing a Category, you ensure that your function appears in the correct section of the Blueprint context menu, making it easy for designers to find.
Essential Rules for Implementation
- The Header File: Ensure your function is marked with
UFUNCTION(BlueprintCallable). - The Source File: You still write the logic in your
.cppfile exactly as you would a normal C++ function. - Blueprint Interaction: Once you compile your code, you can right-click anywhere in a Blueprint's Event Graph and search for "HealPlayer." It will appear just like any native Blueprint node.
- Parameters: Any parameters you define in your C++ function (like
float Amount) will automatically become input pins on the Blueprint node.
By using this approach, you effectively move logic into the high-speed C++ layer while retaining the design-time agility that Unreal Engine is famous for. You no longer have to choose between performance and ease of use; you get the best of both worlds.
Key Takeaway: Use the BlueprintCallable specifier within a UFUNCTION macro to allow Blueprint-based actors to execute your custom C++ logic seamlessly.