Unhandled exception when running code

The program is meant to log a message whenever the player presses a key, but when i run the code , i get the following error.

Unhandled exception at 0x000007FEE193791A (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x00000000000000B8.

The code is:

#include "ClientMessage.h"
#include "MyPawn.h"


AMyPawn::AMyPawn(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	SetupInputComponent();
}
void AMyPawn::SetupInputComponent()
{
	InputComponent->BindAction("printStuff", IE_Pressed, this, &AMyPawn::printMessage);
}
void AMyPawn::printMessage()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on screen message!"));
}

And this is the header file:

#pragma once

#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

/**
 * 
 */
UCLASS()
class CLIENTMESSAGE_API AMyPawn : public APawn
{
	GENERATED_UCLASS_BODY()
	void SetupInputComponent();
	void printMessage();
	
	
};

yes i did.

my SetupInputComponent wasnt overridden from the base class , because when i did i got a build error saying that the base class did not contain a function named SetupInputComponent.

That fixed it , thanks.

Did you add in the Edit->Project Settings->Input->Action Mappings the printStuff and set it to the buttons you want it to register?

Also call Super::SetupInputComponent(); in the beginning of your setupinputcomponent function.

Not sure but shouldn’t this input handling be in a player controller? And if you move it there tag it as virtual void SetupInp…

Sorry ignore this and try first setting your functions to public.

Same happened for me. The AGameController constructor is too early for registration, because the InputComponent was nullptr, because AActor::EnableInput(APlayerController* PlayerController) is not called. I tried that manually, now this is not throwing an exception, but did not work (4.7.2):

UCLASS()
class BUTTONFOOTBALL_API AGameController : public APlayerController
{
	GENERATED_BODY()
	
public:
	AGameController(const FObjectInitializer& ObjectInitializer);
	virtual void SetupInputComponent() override;
	void OnFirePressed();
};

.

AGameController::AGameController(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	SetupInputComponent();
}

void AGameController::OnFirePressed()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s"), __FUNCTION__));
}

void AGameController::SetupInputComponent()
{
	AActor::EnableInput(this);
	InputComponent->BindAction("F", IE_Pressed, this, &AGameController::OnFirePressed);
}

Any ideas?

(and how can set the syntax highlighting for this code?)

Move it to BeginPlay instead of constructor perhaps.

The constructor isn’t the problem. I already tried the beginplay and the InputComponent was nullptr… If I call the hack(??) (AActor::EnableInput(this)), I got some assert and it did not response to my action

Is that controller possessing or spawning a pawn? You should be feeding the input you get to a pawn you are controlling or referencing instead of calling that function.

link text, read up a bit about pawns and controllers to get an idea of what is happening. I haven’t looked it up but I believe you are referencing a non static function in a static way, ie what actor are you trying to enable input on, and what are you trying to do as a whole. There are many examples out there that can help you get started on setting up controllers.