Editor crash on startup after build

I just started writing my first lines of C++ code for UE4, and I’ve run into yet another problem.

I started from a blank code project with the basic gamemode and playercontroller classes. I thought I’d see how the new Input system works, so I added a axis in my project’s settings:

AxisMappings=(AxisName="ATilt",Key=Tilt,Scale=1.000000)

Looks simple, right? So I went on to binding this new axis to a function:

AMyGamePlayerController::AMyGamePlayerController(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	InputComponent->BindAxis("ATilt", this, &AMyGamePlayerController::ProcessTilt);
}

in the function I have

void AMyGamePlayerController::ProcessTilt(float Value)
{
	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::SanitizeFloat(Value));
}

So I then built the code, and started the editor. It crashed almost instantly after trying to open my project (it does get to the project dialog). I did some messing around, and I found that the BindAxis call is what’s causing the problem. If I comment out the BindAxis call, the code compiles and the editor runs normally. So I thought I might have done something bad with the BindAxis call, so I did this:

void AMyGamePlayerController::execBindAxis()
{
	InputComponent->BindAxis("ATilt", this, &AMyGamePlayerController::ProcessTilt);
}

and then I called the exec function from the editor’s console. It works like you’d expect it to, so I’m guessing the problem is not with my function call. Question is, what IS the problem?

Here’s the .h file just in case:

#pragma once

#include "GameFramework/PlayerController.h"
#include "MyGamePlayerController.generated.h"

/**
 *
 */
UCLASS()
class AMyGamePlayerController : public APlayerController
{
	GENERATED_UCLASS_BODY()

	UFUNCTION()
	void ProcessTilt(float Value);

	UFUNCTION(exec)
	void execBindAxis();
};

And here’s the editor log file.


UPDATE: I just realized I should bind input in the SetupInputComponent function, so now I have this:

// .h

virtual void SetupInputComponent() OVERRIDE;

// .cpp

void AMyGamePlayerController::SetupInputComponent()
{
	InputComponent->BindAxis("ATilt", this, &AMyGamePlayerController::ProcessTilt);
}

This time it crashes when I start the game (i.e. press the play button).

PS. sorry if I’m asking too many questions, I’m just excited to start moving my project over to UE4 as soon as possible.

I’m guessing InputComponent hasn’t been set-up when you’re trying to use it.

What happens if you put your code in an override of PostInitializeComponents? See AActor::PostInitializeComponents for what you’re overriding, and remember to call Super::PostInitializeComponents();

Thanks a ton, that did it! A combination of my own SetupInputComponent and your reminder to call the base class version solved the problem. Wonder how I forgot to call the Super version? This is what happens when you spend time away from UDK and inside Unity I think…

Based on your edit, SetupInputComponent looks like the correct function to override, however you need to call the base class version so that InputComponent is initialised.

Super::SetupInputComponent();