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).