I’d like to know what is the difference between the 2 methods stated in the title.
USphereComponent* Component = NewObject<USphereComponent>(this);
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
CreateDefaultSubobject in constructor (you need to decler component in constructor in order to see them in extended blueprint, other wise you creating component dynamicly), NewObject everywhere else
I use the fact Katianie posted comment to extend the anwser. I never investigate that but my guess is CreateDefaultSubobject is designed to spawn component, because content of constructor is exclusivly used in Class Default Object (CDO) which could not have secundery objects spawned, CreateDefaultSubobject don’t spawn object but leaves information to create component object on spawn.
From what I understand, NewObject creates and instance that is not visible to editor however instance created by CreateDefaultSubobject is visible to editor. For example, if you create instance of camera using CreateDefaultSubobject and attach it to your pawn, then that camera will actually appear as child of pawn in editor. This is also why CreateDefaultSubobject usage is restricted to constructor.
But it is not editable and also you can’t modify some stuff on them. They appear as Inherited components… a little hidden there. Sometimes they don’t come up unless it’s the rootComponent… (4.18)
if we want create something dynamically or in other word creating in Run-time we are using newobject but createdefaultsubject it is not for creating something dynamically we create it once.