Skip to content

Object Pointers

When you are dealing with managed objects, i.e., objects that derive from UObject and are tracked by the engine's Garbage Collector, standard C++ pointers might get you into trouble. Instead, consider using the following:

Object Pointer

TObjectPtr<T> can be used to declare Hard Object Pointers as non-static class properties. These pointers identify the holders or dependents of the referenced object. The garbage collection system will not destroy an object unless all hard pointers to the object are gone. If an object is explicitly destroyed, all hard pointers to it will be set to nullptr.

Tip

Standard C++ pointers, when paired with UPROPERTY, are treated as Hard Object Pointers.

Weak Object Pointer

TWeakObjectPtr<T> do not indicate a dependency on the referenced object. These Weak Object Pointers are not tracked by the Garbage Collector, and it might destroy the object even when there are weak pointers to it.

They are not nulled when the object is destroyed, but by calling the Get() method, they can ask the GC system whether the target object is live, and return nullptr if it is not. This extra bit of work makes them more expensive to use than hard pointers.

Soft Object Pointer

TSoftObjectPtr<T> are used to define a Soft Object Pointer to an object, which might not have been loaded into memory yet. They hold an internal reference to an asset, and can load the asset if needed by calling the LoadSynchronous() method.

IsPending() would be true if the asset reference is valid but the pointer is not. Note that, like weak pointers, soft pointers will not prevent the target object from being garbage collected. It may be useful to pin the object instance with a hard object pointer once it is loaded, to keep the object instance in memory.