The original tutorial is based on UE 4.18, I am based on UE 4.25.

 

English original Address

In this tutorial, let’s add an Action keyboard response for the role. First, we need to add an input option called Action and bind it to the keyboard input or controller button. In this case, we will bind the Action input to the F key on the keyboard. Go to Edit > Project Settings. Then select the Input option. Click the plus sign next to Action Mappings. Call the new input Action and select F from the drop-down menu.

 

In the xxxcharacter.h file, add the OnAction method under the OnFire method.

 

protected:
	
	/** Fires a projectile. */
	void OnFire();

	// on action 
	void OnAction();
Copy the code

Next, in xxxCharacter. CPP file, we will find SetupPlayerInputComponent function, and connect the Action mapping and OnAction function. We’re going to create the OnAction function in a second. I connect the controller to the OnAction function via the BindAction function in the PlayerInputComponent. In this example, the OnAction function is called every time the keyboard F is pressed

 

PlayerInputComponent->BindAction("Action", IE_Pressed, this, &AUnrealCPPCharacter::OnAction);
Copy the code

Finally, we’ll add the OnAction function. This will be a very simple function for logging messages to the screen.

void AUnrealCPPCharacter::OnAction() { if (GEngine) { GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("I'm Pressing Action")); }}Copy the code

After the game is running, press the F key

 

\