How to do it...

  1. Create a C++ class and select Character as the parent class. Then, hit Next:

  1. Under the Name property, type in Warrior, and then click on Create Class:

We will dive into doing the implementation after we do some setup inside UE4.

  1. Launch UE4 and right-click on the Warrior class. Then, select Create Blueprint class based on Warrior:

  1. From the menu that pops up, set the name to BP_Warrior and then select Create Blueprint Class:

  1. Close the Blueprints menu that was just opened.
  2. Create and select a new Blueprint for your GameMode class, by going to Settings | Project Settings | Maps & Modes:

  1. Click on the + icon beside the default GameMode drop-down menu, which will create a new Blueprint of the GameMode class. Put a name of your choice (say, BP_GameMode):

  1. Double-click the new BP_GameMode Blueprint class that you created to edit it. It can be found in the Contents\Blueprints folder from the Content Browser.
    1. Once your BP_GameMode blueprint is opened, select your Blueprinted BP_Warrior class as the Default Pawn Class:

    The location of the Default Pawn Class property.
    1. To set up the keyboard's input, which will drive the player, open Settings | Project Settings | Input. (Input can be found under the Engine subsection.) In the following steps, we will complete the process that drives the player forward in the game.
    2. Click on the + icon beside the Axis Mappings heading:

    Axis Mappings supports continuous (button-held) input, while Action Mappings supports one-off events.
    1. Give a name to the Axis mapping. This first example will show how to move the player forward, so name it something like Forward.
    2. Underneath Forward, select a keyboard key to assign to this Axis mapping, such as W.
    1. Click on the + icon beside Forward and select a game controller input to map so that you can move the player forward (such as Gamepad Left Thumbstick Up):

    1. Complete Axis Mappings for Back, Left, and Right with keyboard, gamepad, and, optionally, mouse input bindings for each:

    1. Now, return to the .h file. We will need to add some new function definitions, which we will be writing:
    #pragma once

    #include "CoreMinimal.h"
    #include "GameFramework/Character.h"
    #include "Warrior.generated.h"

    UCLASS()
    class CHAPTER_06_API AWarrior : public ACharacter
    {
    GENERATED_BODY()

    public:
    // Sets default values for this character's properties
    AWarrior();

    protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent*
    PlayerInputComponent) override;

    // Movement functions
    void Forward(float amount);
    void Back(float amount);
    void Right(float amount);
    void Left(float amount);

    };
    1. From your C++ code, override the SetupPlayerInputComponent function for the AWarrior class, as follows:
    #include "Components/InputComponent.h"

    // ...

    // Called to bind functionality to input
    void AWarrior::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    check(PlayerInputComponent);
    PlayerInputComponent->BindAxis("Forward", this,
    &AWarrior::Forward);
    PlayerInputComponent->BindAxis("Back", this, &AWarrior::Back);
    PlayerInputComponent->BindAxis("Right", this, &AWarrior::Right);
    PlayerInputComponent->BindAxis("Left", this, &AWarrior::Left);
    }
    1. Provide a Forward function inside your AWarrior class, as follows:
    void AWarrior::Forward(float amount)
    {
    // Moves the player forward by an amount in forward
    // direction
    AddMovementInput(GetActorForwardVector(), amount);
    }
    1. Write and complete functions for the rest of the input directions, that is, AWarrior::Back, AWarrior::Left, and AWarrior::Right:
    void AWarrior::Back(float amount)
    {
    AddMovementInput(-GetActorForwardVector(), amount);
    }

    void AWarrior::Right(float amount)
    {
    AddMovementInput(GetActorRightVector(), amount);
    }

    void AWarrior::Left(float amount)
    {
    AddMovementInput(-GetActorRightVector(), amount);
    }
    1. Return to Unreal and compile your code. Afterwards, play and game and confirm that you can now move with both the keyboard and the left thumbstick on your gamepad: