Object library in Blueprints or from c++

Is there a way to access the Object Library (created from Content Browser->miscellaneous->Object Library) from Blueprints? Alternatively, anyone seen a good c++ example on how to use it for storing and accessing objects in run-time?

I ended up coding my own blueprint node in c++ for this. For future reference, this is how I solved it:

CBlueprints.h file

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "CBluePrints.generated.h"

/**
 * 
 */
UCLASS()
class TESTPROJECT_API UCBluePrints : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:
		UFUNCTION(BlueprintCallable, Category = "CBluePrintLibrary")
		static TArray<UObject*>  LoadObjectLibrary(const FString& Path, TSubclassOf<UObject> ObjectClass);
	
};

The CBlueprints.cpp file

// Fill out your copyright notice in the Description page of Project Settings.


#include "CBluePrints.h"
#include "Engine.h"


TArray<UObject*> UCBluePrints::LoadObjectLibrary(const FString& Path,  TSubclassOf<UObject> ObjectClass)
{
	TArray<UObject*> Assets;

	UObjectLibrary* ObjectLibrary = UObjectLibrary::CreateLibrary(ObjectClass, false, GIsEditor);
	if (ObjectLibrary != nullptr)
	{
		ObjectLibrary->AddToRoot();
		FString NewPath = TEXT("/Game") / Path;
		int32 NumOfAssetDatas = ObjectLibrary->LoadAssetDataFromPath(NewPath);
		TArray<FAssetData> AssetDatas;
		ObjectLibrary->GetAssetDataList(AssetDatas);

	
		UObject* Asset;

		for (int32 i = 0; i < AssetDatas.Num(); ++i)
		{
			FAssetData& AssetData = AssetDatas[i];
			Asset = AssetData.GetAsset();
			if (Asset)
			{
				Assets.Add(AssetData.GetAsset());
			}
		}
	}
	return Assets;
}

I have been looking for an example like this all day, thank you so much!

Thank you @JohDav! I was searching how to do this as it is not enabled call for blueprints :slight_smile:

Hi Ace! Could you please post your source code and I’ll have a look

I’m trying to build this and I keep getting the error of “incomplete type is not allowed” for the TSubclassOf ObjectClass part of the .cpp file. It is specifically highlighting the TSubclassOf part of the line, any clue why I’m getting this error?

Can this be explained a bit more for blueprint users? I’ve got the C++ class in, but how do I use it? What is the new blueprint node called?

Also what do I need to change in order to make it a 2D Texture element and not an object output?

Hi Kielbasa,
When you have the C++ blueprint library in place as per the code above then you call the LoadObjectLibrary function in the Blueprint Editor. You might need to restart the editor the first time to let it access the library.

The Path parameter is the folder you want to load objects from, in the example below I have my objects stored in a folder Textures right under the Content folder. The Object Class parameter specifies what type of object you want to load, in your case that would be a Texture2D class. The Return Value parameter would then return an array of the objects found. The last step for you is to Cast each object in the array to a Texture2D.

216225-loadlib.png

I will double check this when I get home but my problem was connecting the output to a 2d texture node, it wouldnt allow it even when the class is set to 2d texture.

Do I have to try and get this variable at run time somehow?

To be more specific, this is what I’m trying to do…

In order to do that you first need to Cast the object to a Texture2D.

Worked like a charm! Thanks a ton!

What is the correct sequence for getting a folder of “child actor blueprints” in to an array of actors?

I have a folder with blueprint child actors and the only way they load is if the object class is set to blueprint. There is no “Cast to Blueprint” like there is with “Cast to Texture2D”. Even than it is confusing how to get them in to an array of actors.

My goal… but I know I’m missing something.

I’m not really sure what you want to accomplish here. The UObjectLibrary used implementing this function deals with UObjects and Blueprint does not inherit from that so what you try to do doesn’t work. If your aim is to load different data into your treasure actor (e.g. points reward) you could make a STRUCT that contains that data and then create a DataTable asset based on that STRUCT. But it depends on what you intend to do.

I have it working manually as it is and I suppose that will have to do for now. I was merely trying to auto-mate some of the treasure implementation components.

So the process I was going for is Actors → Array → Spawn Random Actor → Dynamic Detect Actor → Change UI Icons Accordingly.

I have this all working accept for the actors in to the array part, that is done by manually creating an array and adding the actors one by one.

In the end, all I would have to do is make the treasure actor and import the 2D textures in to the correct folders. I wouldn’t have to touch the code at all, so improves implement times and lowers human error!

=)

Hello, total C++ noob here, sorry to hijack this…
I’ve tried to implement this code in a 4.17.2 project which otherwise has no code.
I’m getting tons of errors when trying to build and it looks like there’s some declaration missing:

BlueprintNodeThing.h:

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
/*#include "BlueprintNodeThing.generated.h"

/*
 */
class SOUNDBOARD_API UBlueprintNodeThing : public UBlueprintFunctionLibrary
{
public:
	UFUNCTION(BlueprintCallable, Category = "CBluePrintLibrary")
		static TArray<UObject*>  LoadObjectLibrary(const FString& Path, TSubclassOf<UObject> ObjectClass);
};

BlueprintNodeThing.cpp:

#include "BlueprintNodeThing.h"

TArray<UObject*> UBlueprintNodeThing::LoadObjectLibrary(const FString& Path, TSubclassOf<UObject> ObjectClass)
{
	TArray<UObject*> Assets;

	UObjectLibrary* ObjectLibrary = UObjectLibrary::CreateLibrary(ObjectClass, false, GIsEditor);
	if (ObjectLibrary != nullptr)
	{
		ObjectLibrary->AddToRoot();
		FString NewPath = TEXT("/Game") / Path;
		int32 NumOfAssetDatas = ObjectLibrary->LoadAssetDataFromPath(NewPath);
		TArray<FAssetData> AssetDatas;
		ObjectLibrary->GetAssetDataList(AssetDatas);


		UObject* Asset;

		for (int32 i = 0; i < AssetDatas.Num(); ++i)
		{
			FAssetData& AssetData = AssetDatas[i];
			Asset = AssetData.GetAsset();
			if (Asset)
			{
				Assets.Add(AssetData.GetAsset());
			}
		}
	}
	return Assets;
}

And here’s the errors (unfortunately in German):

[****]\BlueprintNodeThing.cpp(7): error C2065: "UObjectLibrary": nichtdeklarierter Bezeichner
[****]\BlueprintNodeThing.cpp(7): error C2065: "ObjectLibrary": nichtdeklarierter Bezeichner
[****]\BlueprintNodeThing.cpp(7): error C2653: "UObjectLibrary": Keine Klasse oder Namespace
[****]\BlueprintNodeThing.cpp(7): error C3861: "CreateLibrary": Bezeichner wurde nicht gefunden.
[****]\BlueprintNodeThing.cpp(8): error C2065: "ObjectLibrary": nichtdeklarierter Bezeichner
[****]\BlueprintNodeThing.cpp(10): error C2065: "ObjectLibrary": nichtdeklarierter Bezeichner
[****]\BlueprintNodeThing.cpp(10): error C2227: Links von "->AddToRoot" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
[****]\BlueprintNodeThing.cpp(10): note: Typ ist "unknown-type"
[****]\BlueprintNodeThing.cpp(12): error C2065: "ObjectLibrary": nichtdeklarierter Bezeichner
[****]\BlueprintNodeThing.cpp(12): error C2227: Links von "->LoadAssetDataFromPath" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
[****]\BlueprintNodeThing.cpp(12): note: Typ ist "unknown-type"
[****]\BlueprintNodeThing.cpp(14): error C2065: "ObjectLibrary": nichtdeklarierter Bezeichner
[****]\BlueprintNodeThing.cpp(14): error C2227: Links von "->GetAssetDataList" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
[****]\BlueprintNodeThing.cpp(14): note: Typ ist "unknown-type"

Oh and when I uncomment

/*#include "BlueprintNodeThing.generated.h"

I get only this error:

[****]\BlueprintNodeThing.h(5): fatal error C1083: Datei (Include) kann nicht geöffnet werden: "BlueprintNodeThing.generated.h": No such file or directory

Hi Abaris, I think there might be missing a
#include “Engine.h”
in my CBlueprints.cpp file. Try to add that at the top of that file as the second include. I will update my example as well.