Download

Template function for finding assets

Hi

I’ve been trying to make a template function that can find any asset based on a path instead of having to copy&paste the same few lines every time, but I cannot seem to find the right string conversions to do so.

Normally I’d type TEXT( " <path> " ) in the FObjectFinder constructor and it would work but the macro only works with string literals and not with variables.

Any tips on how to make a general asset finder function or macro, do you have other solutions? (or just give up and c&p the lines shamefully every time)


static  T * FindAsset(FString Path) {
  T * Asset;

  static ConstructorHelpers::FObjectFinder<T> AssetFinder(Path);

  if (AssetFinder.Succeeded())
  {
   Asset = (AssetFinder.Object);
   return Asset;
  }
  else {
   return nullptr;
  }
 }

FObjectFinder() requires a const TCHAR, so try passing in a const TCHAR or wchar_t array as your Path instead of a FString. Example shown below.


    TCHAR* Path = L"SoundCue'/Game/GL_AmmoPickup_Cue.GL_AmmoPickup_Cue'";
    wchar_t Path2] = L"SoundCue'/Game/GL_AmmoPickup_Cue.GL_AmmoPickup_Cue'";

    auto AssetFinder = ConstructorHelpers::FObjectFinder<T>(Path);
    auto AssetFinder2 = ConstructorHelpers::FObjectFinder<T>(Path2);

Can ConstructorHelpers be used outside the class constructor functions ? I generally have seen them to be used inside them only.

But there could be cases when you want to use them once the asset is downloaded and saved onto disk and then you want to load it.

Im having problems loading a Custom **Widget **with ConstructorHelpers.
Any alternative way to spawn widgets without this?

Constructor Helpers are there for getting reference from the content folder and then you can create your components accordingly. Dig into it more, you will get idea.

Hey there Don! You wouldn’t want to use ConstructorHelpers for your Widgets. You’d want to use CreateWidget<>().

Here are some helpful links:

See MyHud.h and .cpp down at the bottom of the original post: How I Mix UMG and C++ - Unreal Engine Forums
See section 2. PlayerController: https://wiki.unrealengine.com/UMG,_Referencing_UMG_Widgets_in_Code

Hope this helps, have a great day!

hi, thank you for your help . i finally made it this way


void AMainPlayerController::LoadWidget()
{

    FStreamableManager& AssetLoader = UAssetManager::GetStreamableManager();
    UClass* WidgetClass = AssetLoader.LoadSynchronous<UClass>(FSoftObjectPath("Class'/Game/Blueprints/UI/WBP_Gameplay.WBP_Gameplay_C'"));
    if (WidgetClass)
    {

        gameplayWidget = Cast<USGameplayWidget>(CreateWidget<UUserWidget>(this, WidgetClass));

        if (gameplayWidget)
        {
            gameplayWidget->AddToViewport();

            gameplayWidget->ShowDebugText(GetGameInstance<UMyGameInstance>()->bIsAuth ? FText::FromString("Auth true") : FText::FromString("Auth False"));

        }
    }


}

btw do you know to add a custom Unetdriver class on DefaultEngine.ini?
NetConnectionClassName=?

Not very experienced with this myself, but it sounds like you are looking to set up an Online Subsystem? If so, this should get you headed in the right direction: https://docs.unrealengine.com/en-US/Programming/Online/index.html

btw the idea was to use c++ only