Hello, I wrote a func that finds an asset(blueprint in my case) by name, loads it and returns it like TSubcalssOf. It really usefull for spawning object in blueprints.
TSubclassOf<AShip> UShipUtils::GetShip(const FString & name)
{
auto ships = GetAvailableShips();
if (ships.Find(name) != INDEX_NONE) {
TArray<FAssetData> Assets;
auto lib = LibraryManager::GetLibrary("ships");
lib->GetAssetDataList(Assets);
for (int32 i = 0; i < Assets.Num(); ++i) {
FAssetData& assetData = Assets[i];
if (assetData.AssetName.ToString() == name) {
GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Green,TEXT("AssetFound - "));
assetData.GetAsset();
UBlueprint * bp = Cast<UBlueprint>(assetData.ToStringReference().ResolveObject());
if (bp) {
GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Green,TEXT("cast done"));
if (bp->GeneratedClass->IsChildOf(AShip::StaticClass())) {
return *(bp->GeneratedClass);
} else {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Class isn`t cild of AShip"));
}
} else {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("cast FAILED));
}
}
}
}
return NULL;
}
That code works perfectly in editor, and even if i press Play->Standalone. But if I press Launch, something goes wrong…
There what i find out:
assetData.GetAsset();
returns null if I Launch the game but works fine in standalone mode. So i replace it with StaticLoadObject
And my code now looks like that
//assetData.GetAsset();
auto obj = StaticLoadObject(UObject::StaticClass(), nullptr, *assetData.ToStringReference().ToString());
if (!obj)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Failed to load object"));
UBlueprint * bp = Cast<UBlueprint>(obj);
And it still works fine in editor and standalone, and it even loads the object(obj != NULL) when i launch the game. But only when i launch the game it can`t cast my obj to UBlueprint. What can I do about it?