x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Loading an asset at runtime

Hi,

i'm trying to load an asset at runtime, from my plugin but i'm getting a nullptr in return. I suspect this has to do with incorrect path to an asset.

This is my project dir:

 +Project
     +Content
         +MyFolder
             +MyAsset.uasset

So after packaging, at game startup I've tried:

 FStreamableManager AssetLoader;
     FStringAssetReference AssetRef(FPaths::ConvertRelativePathToFull(FPaths::GameContentDir() / "MyFolder/MyAsset.uasset"));
     auto Asset = AssetLoader.SynchronousLoad(AssetRef);

But the Asset is null (when not using .pak file too)

Is that a correct path? If yes, then why do I get null in return? Note i don't do this from inside the UObject, is that relevant? Also, is it possible to load an asset from the .pak file the same way?

Thanks for any help

Product Version: UE 4.8
Tags:
more ▼

asked Aug 11 '15 at 05:09 PM in Packaging & Deployment

avatar image

twxyz
66 3 7 23

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

Ok, it seems like the path to assets should be

 "/Game/PathRelativeToContentFolder"

without the .uasset extension.

Then FStreamableManager works fine, for packaged and compressed content too.

more ▼

answered Aug 11 '15 at 05:58 PM

avatar image

twxyz
66 3 7 23

avatar image trebconnell Jul 14 '17 at 06:17 PM

As of 4.16 I now have to use "/Game/Asset.Asset" instead of "/Game/Asset" which worked before. In addition to switching to LoadSynchronous.

(comments are locked)
10|2000 characters needed characters left

Loot at my answer https://answers.unrealengine.com/questions/257584/staticloadobject-in-packaged-game.html Or another solution - https://answers.unrealengine.com/questions/246115/how-to-find-blueprints-by-parent-bp.html

  #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;
   }

more ▼

answered Aug 11 '15 at 05:15 PM

avatar image

Arthur Khusnutdinov
605 88 93 248

avatar image twxyz Aug 11 '15 at 05:24 PM

That is a lot of code but I would just like to know what I do wrong? What should be the correct path? You're also loading using UObjectLibrary

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question