In this lesson, you will learn how to create a centralized, globally accessible hub of utility functions that any Blueprint in your project can call without needing a specific object reference.
What is a Blueprint Function Library?
Imagine you have a specific piece of logic—like a complex mathematical calculation, a data formatting task, or a specific way to play a sound—that you need to use in your Player Character, your UI widgets, and your enemy AI. Instead of duplicating that logic in every single Blueprint, you can bundle it into a Blueprint Function Library.
These libraries act as a global "toolbox." Because they are static, you do not need to "spawn" or "reference" them in the world to use them. Once created, they appear in the right-click context menu of the Blueprint graph, ready to be used anywhere.
Creating the Library
To create one in C++, you derive a class from UBlueprintFunctionLibrary. When you write your functions inside this class, you must mark them with the UFUNCTION macro and include the BlueprintCallable or BlueprintPure specifier.
Here is what the header file looks like for a simple math utility:
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyMathLibrary.generated.h"
UCLASS()
class UMyMathLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// This function will now appear in any Blueprint under "My Math Library"
UFUNCTION(BlueprintCallable, Category = "Utilities|Math")
static float CalculateCustomPercentage(float CurrentValue, float MaxValue);
};
Why Use These?
- Centralization: If you need to change how a calculation works, you update the C++ code in one place, and every Blueprint using that node is updated instantly.
- Organization: By using the
Categoryspecifier in yourUFUNCTIONmacro, you can group your functions neatly in the Blueprint editor’s right-click menu, making your tools easier to find. - Efficiency: They are static, meaning they are lightweight and do not require the overhead of instantiating an object just to perform a helper task.
Key Takeaway: Blueprint Function Libraries provide a clean, globally accessible way to share reusable logic across your entire project, keeping your individual Blueprints focused and clutter-free.