I’m currently trying to set up a PlayerController using C++, but have run into an issue quite early on.
When I call GetPawn() in the Playercontroller class, it returns NULL and so I can’t possess the pawn and get the movement working.
I’ve tried everything that I can find on google and variations of them to try and get it to work but have had no luck
I want to use a PlayerController as I want to make the code as modular as possible, and also make it so that different pawns can have different control types. So for example a split screen, will use the same character class, but one character will use a controller input and another keyboard and mouse.
my PlayerController.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/PlayerController.h"
#include "MainCharacterKeyboard.generated.h"
/**
*
*/
UCLASS()
class THEGAMECHARACTERTEST_API AMainCharacterKeyboard : public APlayerController
{
GENERATED_BODY()
AMainCharacterKeyboard(const FObjectInitializer& ObjectInitializer);
APawn * Pawn;
void MoveForward(float Value);
virtual void SetupInputComponent() override;
};
And Playercontroler.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "TheGameCharacterTest.h"
#include "MainCharacterKeyboard.h"
#include "Engine.h"
AMainCharacterKeyboard::AMainCharacterKeyboard(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{
Pawn = GetPawn();
Possess(Pawn);
};
void AMainCharacterKeyboard::SetupInputComponent()
{
Super::SetupInputComponent();
check(InputComponent);
if (InputComponent != NULL)
{
InputComponent->BindAxis("MoveForward", this, &AMainCharacterKeyboard::MoveForward);
}
};
void AMainCharacterKeyboard::MoveForward(float Value)
{
GEngine->AddOnScreenDebugMessage(-1, 0.005f, FColor::Green, FString::Printf(TEXT("Moveing %f"), Value));
if (Value != 0.0f && (Pawn != NULL))
{
Pawn->AddMovementInput(Pawn->GetActorForwardVector(), Value);
//FRotator const ControlSpaceRot = GetControlRotation();
// transform to world space and add it
//GetPawn()->AddMovementInput(FRotationMatrix(ControlSpaceRot).GetScaledAxis(EAxis::Y), Value);
}
};
I don’t want to use blueprints because they aren’t flexible later on in the development cycle, and if I use the C++ i’ll be able to understand what’s happening better and thus if bugs come up i’ll have a better knowledge of how to fix them.
I also think that as we get a larger project, if we have mixed blueprints and code, if someone else joins the project then it will be more complicated for them to understand what is happening.