Pawn not possessed?

When my PlayerController tries to call a function from the possessed Pawn , the pawn isn’t possessed anymore , but it was in the constructor

-When i start the game, it prints the string as called in the constructor right after the Possess(MyPawn);

-But when in the “MouseWheelUp” function, it returns a nllptr when the PlayerController checks for possession before calling the function

-The string is still printed at the start of the game , meaning the function has been called successfully so the pawn was possessed at that point

Pawn.h

UCLASS()
class MYPROJECT_API APlayerPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	APlayerPawn();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	void Print(float value);
	void PitchCamera(float AxisValue);

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

protected:
	UPROPERTY(EditAnywhere)
		USpringArmComponent* OurCameraSpringArm;
		UCameraComponent* OurCamera;
		 
		FVector2D CameraInput;
		float ZoomFactor=400;
		
};

Pawn.cpp

APlayerPawn::APlayerPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	//Create our components
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
	OurCameraSpringArm->SetupAttachment(RootComponent);
	OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
	OurCameraSpringArm->TargetArmLength = 400.f;
	OurCameraSpringArm->bEnableCameraLag = true;
	OurCameraSpringArm->CameraLagSpeed = 3.0f;
	OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
	OurCamera->SetupAttachment(OurCameraSpringArm, USpringArmComponent::SocketName);
	

}

// Called when the game starts or when spawned
void APlayerPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void APlayerPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	{
		FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
		NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -70.0f, -15.0f);
		OurCameraSpringArm->SetWorldRotation(NewRotation);
	}

	ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 6.0f);
	OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f+ZoomFactor*8, ZoomFactor);
	
}

// Called to bind functionality to input
void APlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	InputComponent->BindAxis("CamPitch", this, &APlayerPawn::PitchCamera);
}

void APlayerPawn::PitchCamera(float AxisValue)
{
	CameraInput.Y = AxisValue;
	ZoomFactor += AxisValue/10;
	NewLen += ZoomFactor;

}

void APlayerPawn::Print(float value) {

	if (GEngine) {
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("pawn possessed, value:%f"), value));
	}
}

PlayerController.h

UCLASS()
class MYPROJECT_API ARTSPlayerController : public APlayerController
{
	GENERATED_BODY()

		ARTSPlayerController();

public:

	APlayerPawn *MyPawn;
		void SetupInputComponent() override;
		void WheelUp();
		void WheelDown();
		void WheelZero();
		float Zoom=0;
};

PlayerController.cpp

ARTSPlayerController::ARTSPlayerController()
{
	bEnableClickEvents = true;
	bShowMouseCursor = true;
	bEnableTouchEvents = true;
	
	SetPawn(MyPawn);
	Possess(MyPawn);
	MyPawn->Print(Zoom);
}

void ARTSPlayerController::SetupInputComponent() {

	Super::SetupInputComponent();
	this->InputComponent->BindAction("WheelUp", IE_Pressed, this, &ARTSPlayerController::WheelUp);
	this->InputComponent->BindAction("WheelDown", IE_Pressed, this, &ARTSPlayerController::WheelDown);
	this->InputComponent->BindAction("WheelUp", IE_Released, this, &ARTSPlayerController::WheelZero);
	this->InputComponent->BindAction("WheelDown", IE_Released, this, &ARTSPlayerController::WheelZero);
	
}

void ARTSPlayerController::WheelUp() {

	Zoom = 1.0f;
	if (MyPawn != nullptr) {
		MyPawn->Print(Zoom);
		MyPawn->PitchCamera(Zoom);
	}
	else {
		if (GEngine) {
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("not possessed")));
		}
	}
}
void ARTSPlayerController::WheelDown() {

	Zoom = -1.0f;
}
void ARTSPlayerController::WheelZero() {

	Zoom = 0.0f;
}

Hi Ale1221,

You should not be manually possessing your pawns, especially in the constructor, like you are doing. Is there a reason you are doing it this way?

Because i want to possess a specific pawn as soon as i start the game. Is it wrong to call it in the constructor? Should i spawn and possess the pawn in the gamemode?

so i will set up the possession in the GM , thanks!

Hi Ale1221,

Yes, the constructor should only be used to set default values for properties and (in some cases) assign listeners for events. The flow for possessing your pawns is to set the default pawn and player controller classes in your game mode. Possession will automatically be handled for you.