Why is FObjectFinder always static?

All the UE4 C++ template examples and pretty much all of the sample code I’ve ever seen uses a syntax like this to “get” a mesh and assign it as a component:

    static ConstructorHelpers::FObjectFinder<UStaticMesh> mesh_finder(TEXT("StaticMesh'/Path/To/Mesh.Mesh'"));
    mesh_component = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("component_name"));
    mesh_component->SetStaticMesh(mesh_finder.Object);

Clearly, the “FObjectFinder” class is doing some behind-the-scenes magic. I probably don’t want to know all the gory details of that magic, but I am at a point where I need to at least understand:

  1. Why is FObjectFinder always declared as “static?”

  2. Why is this only allowed in a constructor?

I’m asking because I’d like to have a base class and a tree of derived classes. The base class would handle all of the initialization code above, while the derived classes would pass only a string containing the path (e.g. “StaticMesh’/Path/To/Mesh_1.Mesh_1’”). If I understand the C++ “static” keyword correctly, this approach is going to run into problems because all the derived objects will be sharing a single copy of the “mesh_finder” object.

The documentation for FObjectFinder is nonexistent, as far as I can see.

I wrote up a very long answer but it got lost when posting, so I’m attaching it as a text file.

Actually, it’s not specifically answers to your questions, but it covers what you want to do (have base class do the heavy lifting and child classes just set their specific references), as I have also run into this problem before.

Hope it helps.link text

Thanks a lot, that example code is really helpful. I figured there must be an alternative to ConstructorHelpers::FObjectFinder, and probably would have found it if I had poked through the UE source code. :slight_smile:

I think I might expand on your idea and make something like a “mesh manager” class. It would “find” all the meshes using the string paths, then just store the pointers in an array for fast lookup.

Had the same problem a moment ago. All you need to do is just remove the static keyword if you need to have separate mesh instances, and set the different assets references in the derived classes, something like:

Base class:

ConstructorHelpers::FObjectFinder<UStaticMesh> ItemMeshAsset(StaticMeshReference);

Derived class:

StaticMeshReference = TEXT("StaticMesh'/Game/Items/Different_Mesh");

Now simply call the base class contructor from derived class, or make a separate function which will execute the ConstructorHelper.

the separate function however must be run at construction time, because ConstructionHelpers call will crash otherwise. not advising to ever use that approach.

nowhere in that example is ConstructorHelpers::FObjectFinder used.