Hello all, I’m trying to get movement down on my game and but every time I try to call a function for my movement component I get a crash and it looks like my PawnMovementComponent is null. Here is my custome Player Controller .h and .cpp
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "PlayerShipController.generated.h"
class APlayerShip;
UCLASS()
class SPACEGAME_API APlayerShipController : public APlayerController
{
GENERATED_BODY()
public:
APlayerShipController();
UPawnMovementComponent* PlayerMovementComp;
virtual void SetupInputComponent() override;
void XAxisMove(float AxisValue);
void ZAxisMove(float AxisValue);
protected:
virtual void BeginPlay() override;
};
#include "GameFramework/Character.h"
#include "GameFramework/PawnMovementComponent.h"
#include "GameFramework/Controller.h"
#include "PlayerCharacter.h"
#include "PlayerShipController.h"
APlayerShipController::APlayerShipController()
{
}
void APlayerShipController::BeginPlay()
{
Super::BeginPlay();
PlayerMovementComp = GetPawn()->GetMovementComponent();
ensure(PlayerMovementComp != nullptr);
}
void APlayerShipController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAxis("XAxis", this, &APlayerShipController::XAxisMove);
InputComponent->BindAxis("ZAxis", this, &APlayerShipController::ZAxisMove);
}
void APlayerShipController::XAxisMove(float AxisValue)
{
PlayerMovementComp->AddInputVector(FVector(1.f, 0.f, 0.f)); //This is where the crash happens
//UE_LOG(LogTemp, Warning, TEXT("MoveUP"));
}
void APlayerShipController::ZAxisMove(float AxisValue)
{
//UE_LOG(LogTemp, Warning, TEXT("MoveDown"));
}
The PawnMovementComponent is modeled off of the DefualtPawn (I put the code into my custom Pawn class) and here are the snippets from the .h and in the .cpp file that I think are important:
//for .h
virtual UPawnMovementComponent* GetMovementComponent() const override;
UPawnMovementComponent* MovementComponent;
//for .cpp
MovementComponent = CreateDefaultSubobject<UPawnMovementComponent, UFloatingPawnMovement>(TEXT("MovementComp"));
MovementComponent->UpdatedComponent = MainComponent;
UPawnMovementComponent* APlayerCharacter::GetMovementComponent() const
{
return MovementComponent;
}
I looked over, and I don’t think I missed anything. Is there anything in particular that would be causing the crash?