Setting up input handling, within player controller, causes a nullptr exception

Hi folks,

I’ve got a fresh player controller class, in which I’ve overridden SetupInputComponent. In my SetupInputComponent method, I use the InputComponent to BindAxis for several different things, and I specify which method to call …



void AMasterChiefPlayerController::SetupInputComponent()
{
    Super::SetupInputComponent();

    InputComponent->BindAxis("Turn", this, &AMasterChiefPlayerController::OnAxisInputTurn);
    InputComponent->BindAxis("LookUp", this, &AMasterChiefPlayerController::OnAxisInputLookUp);
    InputComponent->BindAxis("MoveRight", this, &AMasterChiefPlayerController::OnAxisInputMoveRight);
    InputComponent->BindAxis("MoveForward", this, &AMasterChiefPlayerController::OnAxisInputMoveForward);
}


When I fire up the game, I immedietly get an error. The call stack shows that the error is coming from OnAxisInputTurn. The error is …


Exception thrown: read access violation.
this->Pawn was nullptr.

… and then from the UE4 editor …


Access violation - code c0000005 (first/second chance not available)

UE4Editor_Halo!AMasterChiefPlayerController::OnAxisInputTurn() [c:\users\ryan\projects\unrealshooter\source\halo\playercontrollers\masterchiefplayercontroller.cpp:16]
UE4Editor_Engine!UPlayerInput::ProcessInputStack() [d:\build\++ue4+release-4.19+compile\sync\engine\source\runtime\engine\private\userinterface\playerinput.cpp:1238]
UE4Editor_Engine!APlayerController::ProcessPlayerInput() [d:\build\++ue4+release-4.19+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2471]
UE4Editor_Engine!APlayerController::TickPlayerInput() [d:\build\++ue4+release-4.19+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:4232]
UE4Editor_Engine!APlayerController::PlayerTick() [d:\build\++ue4+release-4.19+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2135]
UE4Editor_Engine!APlayerController::TickActor() [d:\build\++ue4+release-4.19+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:4327]
UE4Editor_Engine!FActorTickFunction::ExecuteTick() [d:\build\++ue4+release-4.19+compile\sync\engine\source\runtime\engine\private\actor.cpp:134]
...

I’m confused though, because my OnAxisInputTurn method is empty. I make no attempt to reference the Pawn or anything.


void AMasterChiefPlayerController::OnAxisInputTurn(float value)
{

}

Does anybody have a clue why this might be happening? Below is the header for my player controller, as well.


UCLASS()
class HALO_API AMasterChiefPlayerController : public APlayerController
{
    GENERATED_BODY()

public:

    virtual void SetupInputComponent() override;

    UFUNCTION()
    void OnAxisInputTurn(float value);

    UFUNCTION()
    void OnAxisInputLookUp(float value);

    UFUNCTION()
    void OnAxisInputMoveRight(float value);

    UFUNCTION()
    void OnAxisInputMoveForward(float value);

};

Thanks!

I haven’t looked at the code but as a general advice you should put movement input actions in the Character and UI/UMG input actions in the Controller.

Frosty,

I ended up “resolving” the situation by following some advice given to me on discord. That advice being to debug my game from Visual Studio by using F5 to bring up the UE4 editor. There apparently was some issue with the way I was launching the editor, combined with the editor using some old build files or something. I don’t fully understand what was originally wrong, but ensuring that I open UE4 editor via Visual Studio’s F5 debug menu seems to force it to always use latest builds.

Regarding the general advice - I’ve done it both ways. I started with my movement input controls in the Character class. After reading the documentation, though, it seems to suggest more of a model where the Controller handles the input, but calls functions on the Character so the Character still ends up performing the logic but the Controller sets up the bindings.

Does this sound normal or is it more common for the bindings to go into the Character, too?

I guess it would depend on the needs of the game? If different Character classes can have different bindings?

Hi Dude,

From all your post before, that looks like there is no instance of your AMasterChiefPlayerController when the engine try to process input event.

PlayerControllers are designed for human players to control Pawns. The binding is a link between input event and character behavior. And that is why people put it into Character. I don’t think it is a matter of needs. The fact of matter is how to achieve needs under the structure of Unreal. Before the whole engine could be handled by yourself, follow the structure of engine gonna keep you far away from those kind of tricky things.

If your need is about different Character classes and different bindings. The answer is definitely yes. Since they are different classes. Different classes means different implementation.

Just note that if you put everything in the Controller you need to add valid checks for the Character in every call as the Controller can exist without the Character. That triggers the OOP part of my brain to put the movement input in the Character.

What you outline here is good IMHO. I’ve shipped 3 projects in which the player can at-will possess multiple pawns throughout every game mode:

PlayerController: represents the player’s will, key bindings are here to generalized functions like MoveLeft(), MoveRight(), etc. Always has a reference to the Pawn currently being controlled. A MoveLeft() command to the PlayerController relays a MoveLeft() command to the currently possessed Pawn.

Character: each Character/Pawn has its own implementation MoveLeft(), MoveRight(), etc. extending from a base class (or interface). So the player can control a soldier, tank, or UAV and the commands to the pawn are always the same interface.
Result: key bindings are conveniently all in one place and you can control any type of pawn. :cool: