What is TObjectPtr<T>?

UE5 now uses TObjectPtr instead of raw pointer and they said we should use it too, but i cant find any info apart from the migration info they gave.
So for future readers and myself:
What is TObjectPtr?
What does it do?
Why i should use it?
and should i use it regardless of if i understand at all?
Thanks

3 Likes

They did not really explained what it does when they first announced it but now it is pretty straightforward here link

Basically, its behavior is different for editor builds so it can support lazy-loading of objects (only loads them when the pointer is accessed for the first time) and better tracking of referenced assets during cooking, so the object it points to will get cooked (this obviously worked before, but I believe this makes it a little bit easier to manage).

So to answer you

What is it: TemplateClass wrapper around raw c++ pointers
What does it do: Mentioned above
Why should anyone use it: Lazy-loading can (CAN not WILL) speed up working with the class instance if you are not actually using the objects you are referencing, regarding the cooking, it won’t hurt to use it

Yes, use it even if you are not sure what it does. As with most things, it is way easier to have everything running and then start to break things one by one to learn what they do than having to figure out why something does not work because of multiple reasons.

2 Likes

Possibly/hopefully it’ll go the same way as TSubobjectPtr.

Pepperidge farm remembers.

1 Like