UAnimMontage reference in code

Hi!
I’m trying to make reference to UAnimMontage in code. I was trying with

static ConstructorHelpers::FObjectFinder<UAnimMontage> TestMontage(TEXT("AnimMontage'/Game/Testmontage'"));

but it will crash or won’t compile. I’ve found that not only I have problem with this. Any help?

I avoid directly binding to content this way in code. The reason is that you have less control of overriding the content and it can adversely affect your startup times. Any code referenced that way is immediately loaded and anything that references and so on. IMO, a better way is to create a TAssetPtr and then let a Blueprint subclass specify the desired content. Then you can control when that content is loaded by using sync/async loading. There is some good documentation on async loading and in other posts on our Answers site

Thanks for the answer. I’m doing everything to avoid using blueprints but it looks like I need to change my approach.

You don’t have to change your approach, but changing will give you the most flexibility and control in the long run. You also don’t have to create a BP that uses the node graph. It can just be a data only object. We use that pattern a bunch here.

I didn’t use blueprints because I need to spawn object in runtime. I’m digging right now how to do this… if it is possible.

OK. I managed to do this. I have exactly what I need with blueprint use. Thanks once more time joeGraf because I spent whole day messing with my earlier pure code conception :slight_smile:

Yes, you can spawn either a C++ class or BP class at runtime via SpawnActor.