Blueprint Function Libraries in Unreal Engine (UE4/UE5)

When writing blueprints in Unreal, it’s not atypical that you will be replicating the same generic function across many blueprints in your project. It’s not only annoying to replicate this functionality, but if you decide that you want to change the functionality later, you have to repeat that change throughout all of your blueprints! This is exactly what Blueprint Function Libraries are for! With these, you can write the function in a generic library and then from any blueprint in your project you can call this function! Then, if you change the function later it will update across your whole project.

Blueprints vs C++

Writing your Blueprint Function Library in Blueprints or C++ should not result in any changes that are different than the already existing differences between Blueprints and c++. As you are most likely aware c++ will run faster and have access to more functionality that isn’t available in Blueprints.

Making Blueprint Function Libraries with Blueprints

Making these in Blueprints is quite simple! If the code is already written in blueprints, you most likely will be able to copy and paste everything straight into the Library. First you will need to create the Blueprint Function Library in your project directory’s content directory. Right-click->Blueprints->Blueprint Function Library

Now you can open the Blueprint Function Library and we can make a function, for a bad example we will make a custom truncate function. All it does is do the default truncation of a float to integer and return it. We can then, from another blueprint call it, and print out the result.

Our Blueprint Function Library Function

Our test code in our player blueprint that calls the Blueprint Function Library.

And finally we get the output.

And just like this, you can design any functions as you normally would, just without a world context object. You can pass in and do anything you want, and return back a result!

Making Blueprint Function Libraries in C++

If you are familiar with programming, you may already be familiar with utilizing static functions, and Blueprint Function Libraries work in this same way! In fact, the functions in the Blueprint Function Library must be marked as static or they won’t show up in the editor after compilation!

Creating the cpp file is quite simple. Navigate to your C++ Classes folder and then right-click->New C++ Class->Blueprint Function Library. Once you make this file, any of the functions in the file will be available across all of your blueprints.Here’s what your header file should look like:

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "CommonBlueprintHelpers.generated.h"


UCLASS()
class PROJECTNAME_API UCommonBlueprintHelpers : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
};



One of the functions I always write into my C++ blueprint function library is a function to check if we are in the editor. As of Unreal 4.27, you are unable to tell if you are in editor within Blueprints, quite crazy if you ask me. In C++ this functionality is actually quite simple to write! Here is the function definition.

public:
UFUNCTION(BlueprintPure)
static bool IsWithEditor();

Then, for the actual cpp function:

bool UCommonBlueprintHelpers::IsWithEditor()
{
#if WITH_EDITOR
	return true;
#else
	return false;
#endif
}

As you can see, if WITH_EDITOR is true, we will return true, and otherwise it’s false! Really simple, but also really useful to see in blueprints, especially for debug UI or inputs that you want only in the editor (not builds).

Now you can use Blueprint Function Libraries from Blueprint or C++! These can be really useful for repeat functionality to be used across your project, in both cpp and blueprint!


If you found this tutorial helpful and want to help support me in creating this content that I host and publish for free, please consider contributing to my Patreon or Ko-fi!