In this lesson, you will learn how to leverage SoftObjectPtr and TSoftObjectPtr to defer asset loading, preventing massive memory spikes that often occur during initial level startup.
The Problem: Hard References
When you use a standard UPROPERTY() pointing to a UStaticMesh or UMaterial, you are creating a hard reference. The moment your object (such as an Actor) is loaded into memory, Unreal Engine’s dependency graph forces the engine to recursively load every asset linked to that object. In large projects, if one small Actor accidentally references a massive texture set or high-poly mesh, the entire group is pulled into RAM immediately. This leads to long loading times and excessive memory usage.
The Solution: Soft Object References
A SoftObjectPtr acts as a "promise" rather than a direct link. It stores a string-based path to the asset instead of a direct pointer. Because it doesn't point to the object in memory, the asset remains un-loaded until you explicitly request it.
Consider this implementation in C++:
// Instead of a hard reference: UPROPERTY() UStaticMesh* MyMesh;
// Use a soft reference:
UPROPERTY(EditAnywhere)
TSoftObjectPtr<UStaticMesh> MySoftMesh;
void AMyActor::LoadMeshOnDemand()
{
if (MySoftMesh.IsValid())
{
// Object is already in memory
UStaticMesh* LoadedMesh = MySoftMesh.Get();
}
else
{
// Asynchronously load the object only when needed
FStreamableManager& Streamable = UAssetManager::Get().GetStreamableManager();
Streamable.RequestAsyncLoad(MySoftMesh.ToSoftObjectPath(), FStreamableDelegate::CreateUObject(this, &AMyActor::OnMeshLoaded));
}
}
By decoupling the object from its asset dependencies, you can keep your initial memory footprint lean. You load the heavy hitters only when the player enters a specific trigger zone or reaches a relevant gameplay event. This technique is vital for open-world games or complex hubs where loading the entire scene at once is impossible.
Key Takeaway: Replace hard UProperty references with Soft Object Pointers to break dependency chains, allowing you to manually control exactly when assets are loaded into memory.