Creating a Blueprint Function Library in C++

During the development of a project, you will need to create some functions that are needed in different parts of the project, both in C ++ and in Blueprints. For these cases, you can create a Blueprint Function Library in C++ to gather these functions in one place.

Let’s create a simple Blueprint Function Library in our project to show how it works in practice.

In the Content Browser, access the TutoPart3 folder that is inside the C++ Classes folder. Right-click on free space and choose the New C++ Class… option:

On the next screen, choose the Blueprint Function Libray class as the parent class and click the Next button.

In the Name field, write TutoBPFunctionLibrary. In the Path field, keep the default project folder. Click the Create Class button.

Our example will contain only one function, but you can create several functions in a Blueprint Function Library, as long as they are all static.

The name of the example function is GetNumberOfInstances. It returns the number of instances that exist at the current level of the class passed as a parameter.

Add the function declaration to the TutoBPFunctionLibrary.h file as shown in the code below.

#pragma once

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


UCLASS()
class TUTOPART3_API UTutoBPFunctionLibrary : public UBlueprintFunctionLibrary
{
  GENERATED_BODY()

  UFUNCTION(BlueprintCallable, Category = TutoBPLibrary, 
            meta = (WorldContext = "WorldContextObject") )
  static int32 GetNumberOfInstances(UObject* WorldContextObject, 
                                    TSubclassOf < AActor > ActorClass );	
};

The first parameter of the function is WorldContextObject which is a reference to the current level. The WorldContext specifier was used to automatically fill it in the Blueprint node, as explained in the previous article.

In the second parameter, a TSubclassOf was used so that only Actor subclasses are accepted.

Add the implementation of the GetNumberOfInstances() function to the TutoBPFunctionLibrary.cpp file.

#include "TutoBPFunctionLibrary.h"
#include "Kismet/GameplayStatics.h"

int32 UTutoBPFunctionLibrary::GetNumberOfInstances(UObject* WorldContextObject, 
                                                   TSubclassOf < AActor > ActorClass)
{
 TArray< AActor* > InstancesFound;

 UGameplayStatics::GetAllActorsOfClass(WorldContextObject,ActorClass, InstancesFound);
 
 return InstancesFound.Num(); 
}

The GetAllActorsOfClass() function of the UGameplayStatics class was used. This function fills a TArray with the references of the existing instances in the current level of the class passed as a parameter. Our GetNumberOfInstances() function returns the number of elements in the TArray.

Compile the C++ code.

We will use the GetNumberOfInstances() function in Blueprint to see if all the enemy cannons were destroyed. When this occurs, a message will appear on the screen.
Open the Blueprint FirstPersonGameMode in the FirstPersonBP/Blueprints folder. Right-click on the Event Graph, scroll down the list until you find the category Tuto BPLibrary and select the function Get Number Of Instances.

The image below is the Blueprint node that represents our function. In the Actor Class parameter, select the EnemyCannon class.

In the Event BeginPlay, we will create a Timer that will execute a custom event called CheckCannons once per second. The CheckCannons event calls the Get Number Of Instance function. If the number of instances of EnemyCannon is equal to zero, then the Print String node displays a message on the screen. The Blueprint script must look like this:

To add the custom event, right-click on the Event Graph and choose the Add Custom Event option.

Compile the Blueprint and start the game. Destroy all the cannons on the level to see if the message is displayed.

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu