UBlueprintFunctionLibrary class in Unreal Engine

Published on

A base class for any function libraries exposed to blueprints. It is also useful for working with data, files and other properties right in the Editor mode.

Platforma

UBlueprintFunctionLibrary is a very usefull Class when there's a need to e.g. work with data and files right in editor (for their preparation and simpliefied development), without need to switch to runtime.

Creating assets with C++ & UBlueprintFunctionLibrary

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
UCLASS()
class UExampleClass : public <mark>UBlueprintFunctionLibrary</mark>
{
 GENERATED_BODY()

public:
/**
 * Editor Only - Will not work in packaged build.
 *
 * Create an asset.
 *
 * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
 * @param AssetClass	The class of the asset to create
 * @param AssetFactory	Optional. The factory to use to create the asset
 * @param bOutSuccess	If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 *
 * @return The created asset
 */
UFUNCTION(BlueprintCallable, Category = " ... ")
<strong>static UObject* CreateAsset(FString AssetPath, UClass* AssetClass, class UFactory* AssetFactory, bool& bOutSuccess, FString& OutInfoMsg);</strong>
}

.cpp file - CreateAsset function

#include "AssetToolsModule.h" // <mark>AssetTools (Editor Only)*</mark>

UObject* UExampleClass::CreateAsset(FString AssetPath, UClass* AssetClass, UFactory* AssetFactory, bool& bOutSuccess, FString& OutInfoMsg)
{
 // Get the asset tools module
 IAssetTools& AssetTools = FModuleManager::GetModuleChecked&lt;FAssetToolsModule&gt;("AssetTools").Get();

 // Find right factory
 UFactory* Factory = AssetFactory;
 if (Factory == nullptr)
 { 
   for (UFactory* Fac : AssetTools.GetNewAssetFactories())
   {
      if (Fac-&gt;SupportedClass == AssetClass)
      {
         Factory = Fac;
         break;
      }
   }

   if (Factory == nullptr)
   {
     bOutSuccess = false;
     OutInfoMsg = FString::Printf(TEXT("Create Asset Failed - Was not able to find factory for AssetClass. '%s'"), *AssetPath);
     return nullptr;
   }
 }

 // Can factory create the desired asset?
 if (Factory-&gt;SupportedClass != AssetClass)
 {
   bOutSuccess = false;
   OutInfoMsg = FString::Printf(TEXT("Create Asset Failed - Factory cannot create desired AssetClass. '%s'"), *AssetPath);
   return nullptr;
 }

 <mark>// Create asset
 UObject* Asset = AssetTools.CreateAsset(FPaths::GetBaseFilename(AssetPath), FPaths::GetPath(AssetPath), AssetClass, Factory);</mark>

  // Make sure it worked
  if (Asset == nullptr)
  {
    bOutSuccess = false;
    OutInfoMsg = FString::Printf(TEXT("Create Asset Failed - Either the path is invalid or the asset already exists. '%s'"), *AssetPath);
    return nullptr;
  }

  // Return the asset
  bOutSuccess = true;
  OutInfoMsg = FString::Printf(TEXT("Create Asset Succeeded - '%s'"), *AssetPath);
  <strong>return Asset;</strong>
}

* As of AssetToolsModule.h is AssetTools type of dependency, that is not attached to the project by default, it must be added to .Build.cs file manually.

PrivateDependencyModuleNames.AddRange
(
 new string[]
  { 
   // Default Modules
   "Core", 
   "CoreUObject", 
   "Engine", 
   // New Modules - Editor Only
   <mark>"AssetTools"</mark>,
  }
);

Creating various kind of assets

  • World asset

    #include "Engine/World.h" // Engine
    #include "Factories/WorldFactory.h" // <strong>"UnrealEd" (Editor Only) - dependency must be added to .Build.cs file</strong>
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a World asset.</mark>
     *
     * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
     */
    UWorld* UExampleClass::<strong>CreateWorldAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      // Factory
      UWorldFactory* Factory = NewObject&lt;<a href="https://docs.unrealengine.com/5.2/en-US/API/Editor/UnrealEd/Factories/UWorldFactory/" target="_blank">UWorldFactory</a>&gt;();
      Factory-&gt;bInformEngineOfWorld = false;
      Factory-&gt;bCreateWorldPartition = false;
      Factory-&gt;FeatureLevel = ERHIFeatureLevel::ES3_1;
    
      // Create and return asset
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, UWorld::StaticClass(), Factory, bOutSuccess, OutInfoMsg);
      return Cast&lt;UWorld&gt;(Asset);
    }
  • Material asset

    #include "Materials/Material.h" // Engine
    #include "Factories/MaterialFactoryNew.h" //<strong>"UnrealEd" (Editor Only) - dependency must be added to .Build.cs file</strong>
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a Material asset.</mark>
     *
     * @param AssetPath		The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
    */
    UMaterial* UExampleClass::<strong>CreateMaterialAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      // Factory
      UMaterialFactoryNew* Factory = NewObject&lt;<a href="https://docs.unrealengine.com/5.2/en-US/API/Editor/UnrealEd/Factories/UMaterialFactoryNew/" target="_blank">UMaterialFactoryNew</a>&gt;();
      Factory-&gt;InitialTexture = nullptr;
    
      // Create and return asset
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, UMaterial::StaticClass(), Factory, bOutSuccess, OutInfoMsg);
      return Cast&lt;UMaterial&gt;(Asset);
    }
  • LevelSequence asset

    #include "LevelSequence.h" // <strong>"LevelSequence" - dependency must be added to .Build.cs file</strong>
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a LevelSequence asset.</mark>
     *
     * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
     */
    ULevelSequence* UExampleClass::<strong>CreateLevelSequenceAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, ULevelSequence::StaticClass(), nullptr, bOutSuccess, OutInfoMsg);
      return Cast&lt;ULevelSequence&gt;(Asset);
    }
  • NiagaraSystem asset

    #include "NiagaraSystem.h" // <strong>"Niagara" - dependency must be added to .Build.cs file</strong>
    #include "BehaviorTree/BlackboardData.h"
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a NiagaraSystem asset.</mark>
     *
     * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
     */
    UNiagaraSystem* UExampleClass::<strong>CreateNiagaraSystemAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, UBlackboardData::StaticClass(), nullptr, bOutSuccess, OutInfoMsg);
      return Cast&lt;UNiagaraSystem&gt;(Asset);
    }
  • SoundCue asset

    #include "Sound/SoundCue.h" // Engine
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a SoundCue asset.</mark>
     *
     * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
     */
    USoundCue* UExampleClass::<strong>CreateSoundCueAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, USoundCue::StaticClass(), nullptr, bOutSuccess, OutInfoMsg);
      return Cast&lt;USoundCue&gt;(Asset);
    }
  • Enum asset

    #include "Engine/UserDefinedEnum.h" // Engine
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a Enum asset.</mark>
     *
     * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
     */
    UUserDefinedEnum* UExampleClass::<strong>CreateEnumAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, UUserDefinedEnum::StaticClass(), nullptr, bOutSuccess, OutInfoMsg);
      return Cast&lt;UUserDefinedEnum&gt;(Asset);
    }
  • Struct asset

    #include "Engine/UserDefinedStruct.h" // Engine
    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Create a Struct asset.</mark>
     *
     * @param AssetPath	The path of where to create the asset: "/Game/Folder/MyAsset"
     * @param bOutSuccess	If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The created asset
     */
    UUserDefinedStruct* UExampleClass::<strong>CreateStructAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg)</strong>
    {
      UObject* Asset = <a href="#create-asset">CreateAsset</a>(AssetPath, UUserDefinedStruct::StaticClass(), nullptr, bOutSuccess, OutInfoMsg);
      return Cast&lt;UUserDefinedStruct&gt;(Asset);
    }
Vrealmatic consulting

Anything unclear?

Let us know!

Contact Us