How to update TSubobjectPtr to the current v4.6 changes

Hi all,

I’m new to UE4 and a bit rusty on C++ programming at the moment, I’m trying to follow up on the tutorial and on the new changes regarding the v4.6, I read that TSubobjectPtr<> was deprecated and now we should use pointers instead, so I was trying to understand how to achieve this and I’ll need a bit of help, this is how I’m trying to create my components:


/** Simple collision primitive to use as the root component. */
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Pickup)
USphereComponent BaseCollisionComponent;

First off what do they mean when they say that “We also added public accessor functions for subobjects (components) and deprecated all public subobject properties (we are not going to remove them, but make them private in 4.7).” Quote from Robert M. (second answer in thread). What I get from this is that the UPROPERTY will have to be constructed with a different way to access the parameters in v4.7.

But how do I manage to initialize my var on the .cpp file?, I’m trying
BaseCollisionComponent = FObjectInitializer.CreateDefaultSubobject<[type]>()
but this doesn’t work.

Instead of

TSubobjectPtr<USphereComponent> BaseCollisionComponent;

you can now use standard C++ pointers:

USphereComponent* BaseCollisionComponent

In your example, you just forgot the star after USphereComponent. Your FObjectInitializer line looks correct, and should workonce you fix the star.

BaseCollisionComponent = FObjectInitializer.CreateDefaultSubobject<USphereComponent>();

If you’d like to learn more about C++ pointers, take a look at Pointers - C++ Tutorials (specifically the section on declaring pointers).

No problem. Pointers are pretty complex compared to what you find in other languages (such as Java, C#, or Python), but there’s a lot of documentation out there to help you out.

Awesome, thanks for your help, yes I’m just rusty on pointers and C++ in general, I’ll definitely take a look at that :slight_smile:

Glad you guys went with this instead, coming from standard c++ I was confused about TSubobjectPtr. Thanks for going standard :slight_smile: