Two things i don't quite get

Hey, i’ve been playing around with c++ and there are two things i don’t get.

SQPawn.h


UPROPERTY(Category = Camera, VisibleDefaultsOnly, BlueprintReadOnly)
TSubobjectPtr<class UCameraComponent> PlayerCamera;

SQPawn.cpp


PlayerCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, ASQPawn::CameraComponentName);
PlayerCamera->AttachTo(CameraSpringArm, USpringArmComponent::SocketName);
PlayerCamera->bUseControllerViewRotation = true;

Why does setting up a camera this way work? When is the game told to accually use this camera?


// Structure to hold one-time initialization
struct FConstructorStatics
{
	ConstructorHelpers::FObjectFinderOptional<UStaticMesh> Mesh;
	FConstructorStatics()
		: Mesh(TEXT("/Game/Shapes/Shape_Sphere.Shape_Sphere"))
	{
	}
};
static FConstructorStatics ConstructorStatics;

// Create static mesh component
Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, ASQPawn::MeshComponentName);
Mesh->SetStaticMesh(ConstructorStatics.Mesh.Get());
RootComponent = Mesh;

The struct is what i don’t get here. I think i just don’t understand the syntax.
Could anybody help me understand this? :slight_smile:

Thanks for any help in advance

  • jgm

Hey, i’ve been playing around with c++ and there are two things i don’t get.

So the other half of this is the PlayerController. You can think of it as the actor that actually represents you the player. When it possesses the pawn, a bunch of interaction occurs between the pawn and the controller which eventually leads to binding that pawns camera to the controllers player view. So basically the camera is auto detected when your controller attaches to the pawn.

As for the constructor statics. Gameplay Classes in Unreal Engine | Unreal Engine 5.3 Documentation

Thank you!
Have i gotten this right then; The base class of the PlayerController ask the pawn possessing it if the pawn has an object that is a subclass of UCameraComponent?

It’s related to the view target. You cantransition between view targets from Blueprints (or using the SetViewTarget function directly in C++). When the game starts, it must set the view target to the player by default.

Thanks! But what i still don’t get though is why it works, seeing as I was the one who added the PlayerCamera pointer and the 3 lines in the cpp file are the only times its referenced.

The engine doesn’t know or care about your pointer, it knows about the camera because all components are automatically registered with the actor they’re attached to. It’s this registration system that lets the player controller know there’s a camera on this actor.