Constructor not working for certain properties/functions

For some reason when I try to do the following, the RelativeLocation and SetOwnerNoSee do not work.

AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	// Create the first person camera
	FirstPersonCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
	FirstPersonCameraComponent->AttachParent = GetCapsuleComponent();
	// Position the camera a bit above the eyes
	FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 40.0f + BaseEyeHeight);

	// Hide the 3rd person mesh to the owner
	GetMesh()->SetOwnerNoSee(true);
}

However, if I move those two lines to PostInitializeComponents, they do work.

void AFPSCharacter::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	// Position the camera a bit above the eyes
	// I had to do this here instead of in the constructor. Possible BUG
	FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 20.0f + BaseEyeHeight);

	// Hide the 3rd person mesh to the owner
	// I had to do this here instead of the constructor. Possible BUG
	GetMesh()->SetOwnerNoSee(true);
}

The RelativeLocation call within PostInitializeComponents causes my character to appear to spawn in a different location for a brief time. And then it adjusts to the correct position.

Why don’t these things work in the constructor? :frowning:

Edit: I have been debugging this more and after putting some breakpoints within the constructor it looks like RelativePosition does change to 0,0, 104 (which is correct). And then it is somehow changing to 0, 0, -1939.9729 before PostInitializeComponents. This does not occur in the constructor, so I have no idea why it would be happening.

Hi erebel55,

Thank you for the report. We have assigned one of our technicians to investigate your issue and they may post here with additional questions or comments.

Hey erebel55-

Rather than using RelativeLocation it would be better to use the SetRelativeLocation(FVector) function. As for the “SetOwnerNoSee” I found that setting this in the constructor is actually updating what the default value should be for these options however it does not change the default value itself. If you set this in the constructor and compile the code again, you should be able to find the option in the blueprint defaults and click the yellow arrow next to it to reset the value.

Cheers

Ahh so SetOwnerNoSee(true) in the constructor set’s the default to true but doesn’t actually change it. Clicking the yellow arrow resets it back to default (which is now true). That is a bit confusing, but it makes sense now.

It looks like the relative location problem was the same issue as well. I had to click the yellow reset to defaults arrow in the blueprint for the first person camera’s location.

AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	// Create the first person camera
	FirstPersonCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
	FirstPersonCameraComponent->AttachParent = GetCapsuleComponent();
	// Position the camera a bit above the eyes
	FirstPersonCameraComponent->SetRelativeLocation(FVector(0, 0, 40.0f + BaseEyeHeight));
	// Allow the pawn to control rotation.
	FirstPersonCameraComponent->bUsePawnControlRotation = true;
}

Without clicking the yellow arrow I was getting weird results like this.

However, I’m still getting one issue that only seems to happen to clients (and not the server).
The first person camera and mesh are being spawned at that odd location (way below the third person camera and mesh) for about half a second and then they go back up to where they are supposed to be (I’m assuming it hits the setrelativelocation in the constructor).

I uploaded a video of the issue since that was a mouthful. It’s at :06 of this video - YouTube
Please excuse the awful blurriness (I’m on a horrible gpu)

Hey erebel55-

You’re likely correct that the camera is starting in a default location and then updating to the Set() location after the first tick. Looking at the hierarchy of your blueprint the FirstPersonMesh is a child of the FPCamera. If you swap these two and make the camera a child of the mesh, you can set the camera’s relative location (relative to the mesh) which should take effect when the game starts. If not then updating the location on BeginPlay might help.

I swapped it so that FPCamera was a child of FirstPersonMesh but I’m still getting the same behavior as in the video. I just noticed that when you start at the odd position you can’t see your first person mesh. Which is strange. Do you have any other ideas?

AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{

	// Create the first person mesh (only viewable by owner)
	FirstPersonMesh = ObjectInitializer.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("FirstPersonMesh"));
	// Only the owning player can see this mesh
	FirstPersonMesh->SetOnlyOwnerSee(true);
	// Attach to the first person camera
	FirstPersonMesh->AttachParent = GetCapsuleComponent();
	// Turn shadows off for this mesh
	FirstPersonMesh->bCastDynamicShadow = false;
	FirstPersonMesh->CastShadow = false;

	// Create the first person camera
	FirstPersonCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
	FirstPersonCameraComponent->AttachParent = FirstPersonMesh;
	// Position the camera a bit above the eyes
	FirstPersonCameraComponent->SetRelativeLocation(FVector(0, 0, 40.0f + BaseEyeHeight));
	// Allow the pawn to control rotation.
	FirstPersonCameraComponent->bUsePawnControlRotation = true;
}

I figured out that it’s because a random camera is being spawned at 0,0,0. This camera is because of this https://answers.unrealengine.com/questions/137942/ue-46-preview-cameraactor-created-automatically-wh.html

It’s pretty annoying but at least I know what is going on now.