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