LoadObject vs StaticLoadObject

What is the difference between LoadObject vs StaticLoadObject? For example in

UMaterial *mt = LoadObject<UMaterial>(nullptr, TEXT("/Game/Map/Materials/grass.grass"));
UMaterial *mt = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, TEXT("/Game/Map/Materials/grass.grass")));

And do you know a good reference where I can find such things myself?


This is an old-ish thread, but since I’ve encountered this question in other places a few times and this one comes up in a google search, I thought I’d provide an answer here as well.

If you look at UObjectGlobals.h at the definition of LoadObject<T> you will notice that it is just a wrapper around calling StaticLoadObject before casting it to type T. It does omit the seventh argument bAllowObjectReconciliation, which I’ll explain later. The body of LoadObject<T> is simply:

return (T*)StaticLoadObject( T::StaticClass(), Outer, Name, Filename, LoadFlags, Sandbox );

The following statements are therefore equivalent:

UMaterial* mt1 = LoadObject<UMaterial>(nullptr, TEXT("/Game/Map/Materials/grass.grass"));
UMaterial* mt2 = (UMaterial*)StaticLoadObject( T::StaticClass(), nullptr, TEXT("/Game/Map/Materials/grass.grass"));

You might think that the LoadObject function would add an additional call to the stack, but importantly it is marked ‘inline’ and so these are indeed a direct equivalence.

The LoadObject version saves a few keystrokes, but omits the bAllowObjectReconciliation term.
In order to set this value (default: true) you must call StaticLoadObject directly. Changing this to false will cause the inner workings to skip the pre-emptive call to FindObject thereby requiring a LoadPackage call**.

**Note: LoadPackage would not be called if the process a) requires cooked data or is a server only process and b) GUseSeekFreeLoadaing is true. Under these circumstances disabling bAllowObjectReconciliation will prevent the function from ever obtaining the object since it will not be loaded from disk, and calling find on it is prevented.




Thanks for the info, we would like to see more templates getting utilized as well as this one :]

Compile-time calculations are so so underestimated in UE4 community.

In the second code snippet, is T::StaticClass() meant to be UMaterial::StaticClass()?