Crashing when accessing fields within a method defined in C++

I am currently having trouble accessing a field from a method that is defined in a class. It will fail to access and return a null pointer. I am fairly new with C++ so I may be screwing this up and require some help understanding what is going on.

I have defined a C++ Character Script that contains a field FirstPersonCamera as follows
In the header file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Camera/CameraComponent.h"
#include "PACombatInterface.h"
class UCharacterMovementComponent;
class UCapsuleComponent;
#include "PACharacter.generated.h"

UCLASS(Blueprintable)
class UNTITLEDGAMEPROJECT_API APACharacter : public ACharacter, public IPACombatInterface
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	APACharacter();

	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	USkeletalMeshComponent *FirstPersonMesh;

	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UCameraComponent *FirstPersonCamera;

// ... etc ...

And it’s initialized in the C++ file as follows:

// ... etc ...

	FirstPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("First Person Camera"));
	FirstPersonCamera->SetupAttachment(RootComponent);
	FirstPersonCamera->SetRelativeLocation(FVector(0.0, 0.0, BaseEyeHeight));
	FirstPersonCamera->bUsePawnControlRotation = true;

// ... etc ...

Inside this the c++ implementation I have a QueryMountable method which accesses the FirstPersonCamera to get the rotation as follows:

In the Header:

// ... etc ...

protected:
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Events")
	bool QueryMountable(float QueryDistance, FVector &MountLocation);

// ... etc ...

And in the C++ file as follows:

// ... etc ...
bool APACharacter::QueryMountable_Implementation(float QueryDistance, FVector &MountLocation)
{
	if(bIsCrouched) {
		return false;
	}
	FHitResult HitResult;
	FCollisionQueryParams Params = FCollisionQueryParams::DefaultQueryParam;
	Params.AddIgnoredActor(this);
	if(!FirstPersonCamera)
	{
		GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, "FirstPersonCamera is NULL");
		return false;
	}
// ... etc ...

This method works if the player is currently possessing the character. I do not get the error. However, when the character is possessed by an AI and tries to use this method, the error is printed out. My current code avoids crashing, however I do want my AI to be able to use this mount method. I am not sure why the First Person Camera in this case would return NULL considering that it is initialized in the constructor.

PLEASE NOTE IF RELEVANT: in the engine I have created a blueprint class based off this C++ class. However, I am only using it to set the variables for the mesh and AI controller that is calling this QueryMount function. This Blueprint class is inheriting from a C++ child class of the above class. However again this is only creating some variables that I require for the AI Controller to access. it doesn’t even have a C++ implementation file, only a header file

C++CharacterClass → C++EnemyClass → Blueprint Class

EditAnywhere can cause blueprint corruption when used with components. I use VisibleAnywhere. See if it happens if you create a new blueprint asset. after changing to VisibleAnywhere.

It’s in the c++ constructor right?

I don’t see anything else right now. I doubt the entire camera component would be “optimized away” while an AI uses the pawn.

Here’s another corruption case (the property / compnent name got stuck), this is present in all engine versions afaik:

https://stackoverflow.com/questions/54580713/my-camera-pointer-is-returning-null-when-accessing-the-camera-anywhere-else-but

But, that doesn’t explain why it would work for a player, not an npc. Blueprints just misbehave often and I want to rule out that possibility.

Actually, I am not super certain what EditAnywhere or VisibleAnywhere do. They seem like they do the same for some reason. But that is probably just my lack of understanding. I should probably read up on it a bit more and maybe look at the source code or something.

That being said, I think you were right that it was a corruption issue or something like that. As part of a troubleshooting step I realised that if I rebuilt some of the blueprints from scratch with a new blueprint class, the issue would go away. So I sort of did that for all the scripts to avoid the issue. After that, everything started working perfectly again. So there was definitely something weird going on that was not intentional behaviour.

Thanks for the help.

You can find those specifiers here:

Property Specifiers | Unreal Engine 4.27 Documentation | Epic Developer Community

There are also docs pages for function and class specifiers.