ShooterGame Question

As I’ve been learning UE4, I’ve spent a lot of time poking around the various content projects, especially shooter game, trying to understand The Unreal Way™. I understand that ShooterGame was done back in the 4.0 or 4.1 days and some things have changed, but overall, I’ve learned an awful lot from taking apart the engine.

Every once in a while, though, I see something and I don’t know if there’s a reason for it, to wit:

In the constructor of AShooterCharacter, it creates a subobject for the first person mesh and stores it in the member variable Mesh1P:

AShooterCharacter::AShooterCharacter(const class FPostConstructInitializeProperties& PCIP) 
	: Super(PCIP.SetDefaultSubobjectClass<UShooterCharacterMovement>(ACharacter::CharacterMovementComponentName))
	Mesh1P = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("PawnMesh1P"));
	Mesh1P->AttachParent = CapsuleComponent;
	// etc...

What I don’t understand is later, in OnCameraUpdate(), the code retrieves a pointer to the same subobject:

void AShooterCharacter::OnCameraUpdate(const FVector& CameraLocation, const FRotator& CameraRotation)
	USkeletalMeshComponent* DefMesh1P = Cast<USkeletalMeshComponent>(GetClass()->GetDefaultSubobjectByName(TEXT("PawnMesh1P")));
	// etc...

So, my questions is, is there some reason that Mesh1P doesn’t work in this particular method? It possible that it’s just an example of doing something the hard way. It might’ve been written by a different developer than the rest of the code, or possibly an earlier version of the game created Mesh1P in the blueprint and not in code. I’m really not trying to criticize, but I am trying to understand if there’s a reason for retrieving the pointer here this way that I’m just not seeing.


I think you’re right in that its probably a leftover or doing something the hard way. :slight_smile:

There are a couple of definitions we have to understand first, to comprehend what is happening here.

1- DefaultObject- Each class has an instance of a DefaultObject, that basically has all the data values of a newly constructed object. It allows you to do a lot of neat things, like for example getting the default values of variables, etc.
2- Subobjects- Each object can have Subobjects on them, most of the times they are things like components. An example here is the Meshes under the characters
3- DefaultSubobjects- This are Subobjects that any new instance of the ParentObject will have, as in not added dynamically (through gameplay) but added in the constructor of the class (
Mesh1P = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT(“PawnMesh1P”)); ) So the line that does
USkeletalMeshComponent* DefMesh1P = Cast<USkeletalMeshComponent>(GetClass()->GetDefaultSubobjectByName(TEXT(“PawnMesh1P”)));
Is getting the PawnMesh1P of the default object. It basically is getting the default values of that mesh as it uses them to rotate the Mesh1P of the running instance, as you can see later in the OnCameraUpdate function Mesh1P->SetRelativeLocationAndRotation(PitchedMesh.GetOrigin(), PitchedMesh.Rotator());

Hope that helps

You bumped a 3 year old post…

so? is that a problem? Maybe the problem is that no one answered before.

Well, i did learn something about DefaultObject/Subobjects/DefaultSubobjects , fine by me ^^.