C++ Gamemode Input | BindAction crashes Editor

Im tring to bind the action “ChangeVerse” in my C++ GameMode, yet when its called, it crashes the editor. I had this occur on an actor but manually checking “Auto Receieve Player Input” worked. I want to set this option in C++ but i do not understand the function AutoReceiveInput. The documentation I have found is poor. Any help on using AutoReceiveInput or another solution.

void AMultiversalPuzzleGameMode::BeginPlay()
{
	Super::BeginPlay();
	AutoReceiveInput = EAutoReceiveInput::Player0;
	InputComponent->BindAction("ChangeVerse", IE_Pressed, this, &AMultiversalPuzzleGameMode::ToggleVerse);
}

Are you sure the object InputComponent is not NULL?

How would I assign in it a GameMode or get it?

Are you extending this class from AGameMode?
Check in the source code if InputController gets initialized inside AGameMode else use PlayerController.

Please share the error for the future. I can only guess that you received an “Accessed None” error. This would prove the InputComponent to be NULL.

You shouldn’t use your GameMode for Input. Try to structure your game and use the classes only for the tasks they are made for.

The PlayerController should be the place to bind your functions.

The GameMode is for defining the specific GameSettings. Like “how many kills do i need to win” or “is this deathmatch or capture the flag”.

The problem you’re experiencing is that the InputComponent is NULL inside the GameMode during BeginPlay(), As Anshul mentioned. While the recommended thing to do is to setup input in the PlayerController as eXi mentioned I can understand wanting to setup input from the game mode; so here’s a way it can be done:

void ACustomGameMode::BeginPlay()
 {
     Super::BeginPlay();
     UWorld* World = GetWorld()
     if (World) {
         World->GetFirstPlayerController()->InputComponent->BindAction("CustomEvent", IE_Pressed, this, &ACustomGameMode::CustomFunction);
     }

 }