StaticLoadObject in packaged game

Hello.

I have some code:

for (const FString path : Paths) {


		if (path.Contains(TEXT("forBuildMode"))) {

			if (FPlatformFileManager::Get().GetPlatformFile().FileSize(*path) > 1536) {

				UE_LOG(LogTemp, Warning, TEXT("PATH: %s"), *path);

				FString p = path.Left(path.Find(TEXT("_"), ESearchCase::CaseSensitive, ESearchDir::FromEnd));
				int32 cutLength = p.Find(TEXT("/Content/"), ESearchCase::CaseSensitive) + 9;
				cutLength = p.Len() - cutLength;
				p = p.Right(cutLength);
				p = "/Game/" + p;



				if (!controlArray.Contains(p)) {

					controlArray.Add(p);

					FString pTmp = p + "_Blueprint";

					auto cls = StaticLoadObject(UObject::StaticClass(), nullptr, *pTmp);

					GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("*pTmp ") + pTmp);
					GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("----------------------------- "));

					if (!cls) {
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Failed to load UClass ") + p);
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT( "*pTmp ") + pTmp);
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("----------------------------- ") );
					}

It’s works good, when Game launched from the UE Editor:

But it is not working, when game is packaged:

http://s6.postimg.org/7u5gensdt/screenshot_197.png

My pack settings:

http://s6.postimg.org/4btgi9rht/screenshot_197.png

and

http://s6.postimg.org/89gq7oeb5/screenshot_197.png

(tried both variants).

I noticed, that source file and file in packaged game have different sizes (left panel - packaged game, right panel - source files):

http://s6.postimg.org/6ixp66ws1/screenshot_197.png

Before packaging I run Cook Content for Windows.

What’s happen with my BP after packaging and why it has smaller size after packaging and not working?

Thank you!

1 Like

Answer founded here: StaticFindObjectFast when game is Launched - Blueprint - Epic Developer Community Forums

 #include "debugProj.h"
 #include "ShipFunctionLibrary.h"
 #include "Runtime/AssetRegistry/Public/AssetData.h"
 
 
 UShipFunctionLibrary::UShipFunctionLibrary(const class FPostConstructInitializeProperties& PCIP)
     : Super(PCIP)
 {
 
 }
 
 UObject * MyGetAsset(FAssetData & asset) {
     if (!asset.IsValid())
     {
         return NULL;
     }
 
     UObject * Asset = FindObject<UObject>(NULL, *asset.ObjectPath.ToString());
     if (Asset == NULL)
     {
         FString tmpstring = asset.ObjectPath.ToString();
         UObject * InOuter = NULL;
         ResolveName(InOuter, tmpstring, true, true);
         
         GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Magenta, TEXT("Now string is ") + tmpstring);
         if (InOuter) {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Magenta, TEXT("InOuter->GetName is ") + InOuter->GetName());
             Asset = StaticFindObjectFast(UObject::StaticClass(), InOuter, *tmpstring);
         }
         if (Asset) {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Black, TEXT("WOW I Have Load Something with StaticFindObjectFast!") + Asset->GetName());
             return Asset;
         }
         
         Asset = (UObject *)StaticLoadObject(UBlueprint::StaticClass(), NULL, *asset.ObjectPath.ToString());
         if (!Asset)
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT(" FAILED LOAD Object like BLUEPRINT By StaticLoadObject"));
         else {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Blue, TEXT(" LOADED like BLUEPRINT By StaticLoadObject"));
             return Asset;
         }
 
         Asset = (UObject *)StaticLoadObject(UObject::StaticClass(), NULL, *asset.ObjectPath.ToString());
         if (!Asset)
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT(" FAILED LOAD Object like UOBJECT By StaticLoadObject"));
         else {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Blue, TEXT(" LOADED like UOBJECT By StaticLoadObject"));
             return Asset;
         }
 
         Asset = (UObject *)LoadObject<UBlueprint>(NULL, *asset.ObjectPath.ToString());
         if (!Asset)
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT(" FAILED LOAD Object like BLUERPRINT "));
         else {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Blue, TEXT(" LOADED like BLUEPRINT By LoadObject"));
             return Asset;
         }
 
         Asset = LoadObject<UObject>(NULL, *asset.ObjectPath.ToString());
         if (!Asset)
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT(" EVEN FAILED LOAD Object like UOBJECT "));
         else {
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Blue, TEXT(" LOADED like UOBJECT By LoadObject"));
             return Asset;
         }        
     }
 
     return (UObject*)Asset;
 }
 
 TSubclassOf<AShip> UShipFunctionLibrary::GetShip(const FString & name)
 {
     UObjectLibrary * lib = UObjectLibrary::CreateLibrary(AShip::StaticClass(), true, GIsEditor);
     lib->AddToRoot();
     int32 loaded = lib->LoadBlueprintAssetDataFromPath("/Game/Ships");
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Cyan, FString::FromInt(loaded) + TEXT(" objects loaded for lib - ") + name);
 
     TArray<FAssetData> Assets;
     lib->GetAssetDataList(Assets);
     for (int32 i = 0; i < Assets.Num(); ++i) {
         FAssetData& assetData = Assets[i];
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Get ship: check obj - ") + assetData.AssetName.ToString());
         if (assetData.AssetName.ToString() == name) {
             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("AssetFound - ") + assetData.AssetName.ToString());
 
             if (!MyGetAsset(assetData))
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Get ship: Failed to load object by GetAsset - ") + assetData.AssetName.ToString());
 
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("INFO Class Name - ") + assetData.GetClass()->GetName());
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("INFO FullName - ") + assetData.GetFullName());
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("INFO AssetClass - ") + assetData.AssetClass.ToString());
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("INFO ObjectPath - ") + assetData.ObjectPath.ToString());
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("INFO IsAssetLoaded - ") + (assetData.IsAssetLoaded() ? FString("YES") : FString("NO")));
             GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("INFO IsValid - ") + (assetData.IsValid() ? FString("YES") : FString("NO")));            
                     //UBlueprint * bp = Cast<UBlueprint>(assetData.GetAsset());
             UBlueprint * bp = Cast<UBlueprint>(MyGetAsset(assetData));
             if (bp) {
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Get ship: bleprint cast done for asset - ") + assetData.AssetName.ToString());
                 if (bp->GeneratedClass->IsChildOf(AShip::StaticClass())) {
                     return *(bp->GeneratedClass);
                 }
                 else {
                     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Get ship: Class isn`t cild of AShip- ") + assetData.AssetName.ToString());
                 }
             }
             else {
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Get ship: bleprint cast FAILED for asset - ") + assetData.AssetName.ToString());
             }
         }
     }
     return NULL;
 }
1 Like