Download

Uobjectlibrary always empty?

Hello! I trying to make use of UObjectLibrary, but I seem to be missing something.

I’m using it to get all blueprints of a certain class within a folder.
However if I try to access the list of blueprints it is always empty (and crashes due to out of bounds of trying to work with an array of 0).

I guess I’m setting the default class to look for wrong?

This is my .h

	class UObjectLibrary* EnemyLibrary;

	TArray<FAssetData> SpawnableEnemies;

.cpp

void AEnemySpawner::BeginPlay()
{
	Super::BeginPlay();
if (!EnemyLibrary)
	{
		EnemyLibrary = UObjectLibrary::CreateLibrary(AWSNPC::StaticClass(), false, GIsEditor);
		EnemyLibrary->AddToRoot();
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString("LibraryCreated"));

	}
	EnemyLibrary->LoadBlueprintsFromPath(TEXT("/Game/TwinStick/Gameplay/Enemies"));
	//EnemyLibrary->LoadAssetDataFromPath(TEXT("/Game/Gameplay/Enemies"));

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::FromInt(EnemyLibrary->GetObjectCount()));
	//always returns 0?
	if (!EnemyLibrary->IsLibraryFullyLoaded())
		EnemyLibrary->LoadAssetsFromAssetData();

//otherstuff
}
TSubclassOf<AWSNPC> AEnemySpawner::EnemyToSpawn()
{
	TSubclassOf<AWSNPC> SpawnClass;

	EnemyLibrary->GetAssetDataList(SpawnableEnemies);
	SpawnClass = SpawnableEnemies[FMath::RandRange(0, SpawnableEnemies.Num())].GetClass();

	return SpawnClass;
}

So I’m one step further, I found that I needed to change the second argument in CreateLibrary to true instead of false in order for it to store blueprints. I’ve also replaced GetAssetData with GetObjects. However, now it finds 6 objects instead of the 3 that are actually in the folder?

It works as long as I happen to use the indexes that contains the expected blueprints, but as soon as it tries to read one of the mystery blueprints, it crashes. Any ideas how to make sure that it only loads my genuine blueprints? There doesn’t seem to be any pattern to the classes, I tried removing the first half, the second half, every odd and every even number to see how it duplicates the numbers and which ones are the garbage results, but all combinations resulted in eventual crashes.

Edit: I also tried a similar approach with AssetRegistry, but I can’t find the assetregistry in UE5. There doesn’t seem to be any includes to use, and all the handles to use are undefined. I guess UE5 is based on 4.25, and AssetRegistry was added in 4.26?