TutoProjectCharacter class and InputComponent

The TutoProjectCharacter C++ class represents the player and was created by the Third Person template that also created a Blueprint called ThirdPersonCharacter, located in the folder ThirdPersonCPP/Blueprints. TutoProjectCharacter is the parent class of this Blueprint, which is used to specify the player’s Skeletal Mesh.

The only modification we will do to the TutoProjectCharacter class is to add an Input mapping to allow the player to restart the game by pressing the Enter key.

In the Level Editor, access the Edit->Project Settings… menu and in the Engine category, choose the Input option. Click the + symbol next to Action Mappings, write the name RestartGame for the new Action Mapping and select the Enter key. The Input mapping of the project will look like this:

 

 

The TutoProjectCharacter.cpp file has a function called SetupPlayerInputComponent() where the Input is bound to the C++ functions that will be executed when an Input is triggered. The code below shows some examples:

 

void ATutoProjectCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
  // Set up gameplay key bindings
  check(PlayerInputComponent);
  PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

  PlayerInputComponent->BindAxis("MoveForward", this, &ATutoProjectCharacter::MoveForward);
  PlayerInputComponent->BindAxis("MoveRight", this, &ATutoProjectCharacter::MoveRight);
  
...

The check() function is used to ensure that PlayerInputComponent is valid. If it is invalid, the program execution will stop with an error message.

We will create the RestartGame() function that will be called when the player presses the Enter key. The first step is to add the function declaration in the TutoProjectCharacter.h file. Put it in the second protected: block, below the SetupPlayerInputComponent() function, as shown in this code:

...

protected:
  // APawn interface
  virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
  // End of APawn interface

  void RestartGame();

...
In the file TutoProjectCharacter.cpp bind the Input “RestartGame” at the end of the SetupPlayerInputComponent() function, after the Input “ResetVR”:
...

  // VR headset functionality
  PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &ATutoProjectCharacter::OnResetVR);

  PlayerInputComponent->BindAction("RestartGame", IE_Pressed, this, &ATutoProjectCharacter::RestartGame);
}

Do not forget the & operator before the RestartGame function identification. It is used to return the function’s memory address.

At the end of the TutoProjectCharacter.cpp file, create the RestartGame() function with this content:

void ATutoProjectCharacter::RestartGame()
{
  GetWorld()->GetAuthGameMode<ATutoProjectGameMode>()->StartGame();
}

We are taking a pointer to the instance of Game Mode that is being used by the map and casting for the ATutoProjectGameMode class. But instead of storing the pointer, we’re just using it to call the StartGame() function of the ATutoProjectGameMode class.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed