What is the alternative to ConstructorHelpers:FObjectFinder outside of the constructor?

Hello. When finding an object in the content browser, you can use ConstructorHelpers::FObjectFinder along with the asset’s path, but this will crash when used outside the constructor. What is the equivalent function when I wish to assign a variable after the constructor, such as during BeginPlay() ?

For example, how would I write

ConstructorHelpers::FObjectFinder<UAnimMontage> animationMontage(ANSI_TO_TCHAR(montagePath));

in BeginPlay() ?
Thank you.

Hi . Did part of your reply disappear? It seems you were saying something and only the end was posted.

Hopefully there is a better formal approach these days but my solution was to gather all these hacks (feels that way to a non unreal coder) into the game mode object. In my case there aren’t too many of them so it doesn’t feel too bad.

When it is time for me to cleanup I’ll look at data driving via blueprint.

No - it’s really that simple. I just tried out my cleanup alternative and it works.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Style)
	UClass *MyCoolClass;

Now set your object in blueprint making it easy to either apply logic to it or change it on the fly while removing your hack.

This will NOT be available during your constructor as the property system needs to be initialized. It’s available on BeginPlay and I believe there’s an earlier callback where you can use it but can’t remember what it is.

The FAssetRegistryModule may be what you are looking for.

FAssetRegistryModule* AssetRegistryModule = &FModuleManager::LoadModuleChecked(TEXT(“AssetRegistry”));

link text

No - it’s really that simple.
What is? I have absolutely no idea what hacks you’ve gathered, nor what functions calls you’re referring to.

I was asking about an example of a function call being used to load an asset in code. Additionally, as I did not point it out beyond my tags, I should mention that this is a C++ question and not a blueprint question. We do not use blueprints.

Apologies for misinterpreting your question. The asset registry pointed out by @mikepurvis gives an in depth ability to browse the content tree, you might also be able to use UObjectLibrary::CreateLibrary.

As for hacks - I’ve generally found using the construction helper to find objects by name to be pretty hack like - in my use of it. Many hacks are fine they ship games but often also indicate shortfalls in your technology, sometimes they define the style of the game and contribute directly to its success. We generally aren’t trying to build perfect software but a product that will ship. I don’t mean to cause any offence with the use of the term.

Offtopic: Blueprints are pretty useful fwiw. I’m a solo developer and the majority of my code/ability is in C++ but blueprints have proven high useful for pulling special cases out of C++ and into a place where in the long term someone other than a programmer can change functionality - often on the fly while your game is running or at least without waiting for a recompile. For instance “montagePath” in your example could be easily defined in a blueprint accessible class, tweaked from blueprint etc.

If you need to load UClass* latently (outside constructor), I found this to work:

2 Likes

Your cleanup alternative is not an alternative. I’m dealing with a problem where I can’, simply can’t, load the the mesh from blueprint. It HAS to me done through c++ because it’s an HTTP response, and it HAS ABSOLUTELY to change the object based on what you receive from the HTTP response. so no, this is not al alternative, it’s just a workaround that doesn’t really solve the main issue. WE NEED a we to load meshes after construction

Just make sure you don’t add that constructor code to a base class of the blueprint for which you need the path, or you’ll end up in infinite recursion.