[UE4 / UE5] Question about async loading

I wish to load a class async in c++, what is the common / cleanest way to do this? In my thoughts it would be a “oneliner”:

#include "Async/Async.h"

TWeakObjectPtr<ULazyMe> WeakThis(this);

Async(EAsyncExecution::TaskGraph, [&WeakThis]() {
	if (ULazyMe* StrongThis = WeakThis.Get()) {
		StrongThis->GetLazyContent().LoadSynchronous();
	}
});

Actually surprised there isn’t something like this:

TSoftClassPtr<ULazyThat> That(xxxxxxx);
That->LoadAsync(InDelegate);

However, when I look at the CommonUI code I see a whole lot of funny stuff (overcomplication?). Simplified:

TWeakObjectPtr<ULazyMe> WeakThis(this);

UAssetManager::GetStreamableManager().RequestAsyncLoad(GetLazyContent().ToSoftObjectPath(), [WeakThis]() {
	ULazyMe* StrongThis = WeakThis.Get();
	if (IsValid(StrongThis)) {
		StrongThis->DoStuff(StrongThis->GetLazyContent().Get());
	}
}
, FStreamableManager::AsyncLoadHighPriority
);

Am I supposed to add that bunch where I want something async?

UAssetManager::GetStreamableManager().RequestAsyncLoad(), yes.
Using a lambda, no.
If that’s what you mean.

1 Like

Ok, I’ll use RequestAsyncLoad then :).
As far as I am aware this way of using lambda’s should not cause any trouble? In this situation if the “StrongThis” becomes invalid / collected the lazy load may fail.

Lambda usage is totally up to you.
We use both :stuck_out_tongue:
In the case of our inventory item class, it has one main function gather and load the assets.
Whether or not it needs to load the assets via RequestAsyncLoad(), OnLoadAssetsCompleted() is always called.