Download

How to access default values of Blueprint assets

Hi guys,

I’m trying to get a list of blueprints in a given content folder, and from there to get several properties to populate another asset; but I’m having difficulty obtaining GetDefaultObject()



	FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(FName("AssetRegistry"));
	IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();

	TArray<FAssetData> AssetsList;
	AssetRegistry.GetAssetsByPath(FName("/Game/Objects/Props/Weapons/"), AssetsList, true);

	for (int32 i = 0; i < AssetsList.Num(); i++)
	{
		if (AssetsList*.AssetClass == FName("Blueprint"))
		{
			UE_LOG(LogTemp, Warning, TEXT("Found asset of type: %s"), *AssetsList*.AssetClass.ToString());

			TSubclassOf<AWeapon> Weapon = AssetsList*.GetAsset()->GetClass(); // always NULL
			if (Weapon != nullptr)
			{
				UE_LOG(LogTemp, Warning, TEXT("Found asset: %s"), *Weapon->GetDefaultObject<AWeapon>()->DisplayName); // we don't get this far
			}
		}
	}


GetAsset() returns UObject, and it’s GetClass() returns UBlueprint.

All blueprints in given folder are subclasses of AWeapon actors. How do dereference correctly to TSubclassOf<AWeapon>?

Assignement of UClass* to TSubclassOf<> usually works when I’m dealing with correct blueprints and their parents.

Any help/comment/ideas/suggestions are greatly appreciated!

Sounds like the problem is that “AssetsList*.GetAsset()->GetClass()” returns “UBlueprint::StaticClass()” which of course is NOT a subclass of AWeapon.
Try something like this instead


UBlueprint* BlueprintAsset = Cast<UBlueprint>(AssetsList*.GetAsset());
if (BlueprintAsset)
{
	TSubclassOf<AWeapon> SubclassOfWeapon = BlueprintAsset->GetBlueprintClass();
}

The asset is a Blueprint so its GetClass returns the same as UBlueprint::StaticClass().
The actual class generated by/for the Blueprint is accessible via TheBlueprint->GetBlueprintClass(). I’m not entirely certain this is always the case, but the UClass returned here might even be a UBlueprintGeneratedClass (derived from UClass).

Edit: actually that won’t work. I just checked UBlueprint::GetBlueprintClass and it just returns UBlueprintGeneratedClass::StaticClass() not the actual generated class that you’re looking for. There must be a way to access it although I cannot find it right now.

Edit2: aha found it, UBlueprint is derived from UBlueprintCore which has the following public member:



	/** Pointer to the 'most recent' fully generated class */
	UPROPERTY(nontransactional)
	TSubclassOf<class UObject>	GeneratedClass;

So the code should be something like this now


UBlueprint* BlueprintAsset = Cast<UBlueprint>(AssetsList*.GetAsset());
if (BlueprintAsset)
{
	TSubclassOf<AWeapon> SubclassOfWeapon = BlueprintAsset->GeneratedClass;
}

That’s it!



TSubclassOf<AWeapon> WeaponClass = (UClass*)Cast<UBlueprint>(AssetsList*.GetAsset())->GeneratedClass;


(naturally, with appropriate checks that there are no NULLs along the way).

I had to cast GeneratedClass to UClass…? But it works well.

Thanks!!