Download

Getting an Asset Reference

Hey all,

By assigning a variable’s UPROPERTY as EditAnywhere, we can open that variable to be the Editor so that it can reference a particular asset in your Content folder.

But what if I wanted to skip that? Instead of opening it up to the editor so that I can code it in later, I call the variable reference within the code, the same as it would have been called by the editor?

I’ve looked around and seen that a soft object (or class?) reference might be a way to do it. But it also mentioned that this can cause spikes in the FPS when loading it. Is that really how assets are set by the editor?

For reference, the variable type is a data table. I want to call it every time a component is created to pull its profile settings with an ID (the Row Name).

I appreciate your help!

1 Like

Are you perhaps looking for an FObjectFinder?

static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshRef(TEXT("/Engine/VREditor/TransformGizmo/SM_Sequencer_Node"));
UStaticMesh* StaticMesh = MeshRef.Object;
check(StaticMesh != nullptr);
1 Like

Yes!

That worked great in getting the object.

Thank you again for your help

1 Like

For best practices, C++ land shouldn’t be hard referencing assets/blueprints at all. Specially not in constructors.

Assets should only be loaded by UE via UPROPERTY() references that should be set by the user in a blueprint extending the C++ classes you want to use them in. And for soft loading assets, the soft object path should still be set via UPROPERTY() and not in code. When we do that then assets lifetime and reference practices are followed correctly.

If you circumvent UE’s asset loading system then you are:

  • Adding a big delay before the first frame of the game can be shown since it needs to load all of these before the engine even finishes initializing.
  • Adding all the objects to a permanent object pool that disregards GC and never gets garbage collected.
  • Making the game crash when those objects try to reference anything outside of that disregard pool like when they try to load their own assets later on.