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;
}