CreateDefaultSubobject and pointers

Hello! I’ve been following this tutorial to better understand how UE4 c++ code work and I’ve had some trouble understanding some things with CreateDefaultSubobject and pointers.

in the header file i have

       class USpringArmComponent* OurCameraSpringArm;
       class UCameraComponent* OurCamera;

in the .cpp file

       OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
       OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));

CreateDefaultSubobject seem to be creating an object and we’re assigning it to where OurCameraSpringArm and OurCamera are pointing? But why aren’t we dereferencing them? thanks guys

Because the function “CreateDefaultSubobject” returns a pointer to the object created.
The UObject will be created and managed into the game world, not within your class.

So the CreateDefaultSubobject function creates an object AND returns the address-of the object? I was confused because i didn’t know which one it did but it would make sense that it does both.

Yes. You can think of it that way, though I think it’s more common and perhaps more descriptive to say that it returns a pointer.

By the way, passing objects around by pointers is *extremely *common in UE4 C++, to the point that it’s basically the way to do it.

For example, when you call up GetController() on a pawn, that function returns a *pointer *to the AController that is currently possessing that pawn. Want to get the name of an actor to print it out for debugging? Call up the AActor::GetDebugName(…) function, which takes a pointer to the actor whose name you want to know. Want to apply damage to several actors in a radius around a location? You can use the UGameplayStatics::ApplyRadialDamage(…) function, which (among other parameters) takes a TArray filled with pointers for which actors to ignore.

So it’s to be expected that functions will use pointers to return objects and to accept objects as parameters. It’s the opposite that would be surprising, at least in UE4 C++.

Note that CreateDefaultSubobject should only be used in the constructor, and GetWorld()->SpawnActor(MyClass::UStaticClass(), Location, Rotation) should be used elsewhere. Goes something like this, syntax not exact, but enough to know what I’m referring to.

Thanks that’s really helpful. I jumped from learning c++ to EU4 really quickly so stuff like that sometimes elude me.

I saw that there were a lot of pointers used in Unreal engine but there’s barely any new/delete used, is it because of the garbage collector? Should i worry about not understanding what it does or as long as i don’t use new then the engine will take care of every pointer? Some of the stuff i read in books about pointers said they were to be used carefully but they’re really everywhere in unreal so it got me really wondering.

Thanks i’ll keep that in mind, it’ll probably save me a lot of trouble because i would have used it outside the constructor for sure.