Language:
Page Info
Skill Level:
Engine Version:

2.1 - Making a New Character

During this step, we're going to make a new character using the engine's Character base class. The Character class (derived from the Pawn class) has built-in functionality for bipedal movement; such as walking, running, and jumping.

Adding a Character Class

Although you can manually add *.h and *.cpp files to your Visual Studio solution, it's good practice to use the C++ Class Wizard to add new classes to your project. By using the C++ Class Wizard, the engine creates header and source templates that set-up Unreal specific macros for you.

  1. In the File menu, select New C++ Class... to choose your new parent class.

    SelectNewCPPClass.png

  2. The Choose Parent Class menu will open. Scroll down, select Character as the parent class and click Next.

    ChooseCharacterClass.png

  3. Name the new class "FPSCharacter," then click Create.

    MakeFPSCharacterClass.png

    Now that you've created your FPSCharacter class, you can switch to Visual Studio to add code to your newly created class. FPSCharacter.h and FPSCharacter.cpp will be opened for you and Unreal Engine will automatically compile and reload the code for your new class.

Verifying the Character Class

  1. In the Solution Explorer, expand FPSProject > Source > FPSProject.

    ExpandedSolutionExplorer.png

  2. Double-click FPSCharacter.cpp to open the implementation file for your FPSCharacter class.

  3. Add the following lines of code in the BeginPlay() function to verify that FPSCharacter class is being used:

    if (GEngine)
    {
        // Put up a debug message for five seconds. The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
    }
  4. FPSCharacter.cpp should now look like the following:

    // Fill out your copyright notice in the Description page of Project Settings.
    
    #include "FPSProject.h"
    #include "FPSCharacter.h"
    
    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
        // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;
    
    }
    
    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
        Super::BeginPlay();
    
        if (GEngine)
        {
            // Put up a debug message for five seconds. The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
            GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
        }
    }
    
    // Called every frame
    void AFPSCharacter::Tick( float DeltaTime )
    {
        Super::Tick( DeltaTime );
    
    }
    
    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
    }
  5. Save the FPSCharacter CPP file in Visual Studio.

  6. Locate FPSProject in the Solution Explorer.

  7. Up until now, you've been compiling your project using the editor's Build button. During this step, you're going to gain experience compiling code using Visual Studio's build feature. To compile your code from inside Visual Studio, right-click on FPSProject and select Build to compile your project.

    BuildProject.png

  8. After the build finishes, open Unreal Editor to verify that your newly compiled FPSCharacter class is visible in the Content Browser.

    FPSCharacterContentBrowser.png

Extending your CPP FPS Character Class to Blueprints

Now is a good time to extend the CPP FPS Character class to Blueprints. Please feel free to go to our C++ and Blueprints reference page to learn more about extending C++ classes to Blueprints.

  1. Right-click the FPSCharacter class to open the C++ Class Actions menu.

    CPPClassActionsMenu.png

  2. Click Create Blueprint class based on FPSCharacter to open the Add Blueprint Class dialog menu.

    CreateDerivedBPClass.png

  3. Name your new Blueprint Class "BP_FPSCharacter" and choose the Blueprints folder before clicking the Create Blueprint Class button.

    AddBPClass.png

  4. By now, you should have a newly created BP_FPSCharacter Blueprint Class located inside of the Blueprints folder.

    AddedBPClass.png

  5. Make sure to save your BP_FPSCharacter Blueprint before closing the Blueprint Editor.

Setting the Default Pawn Class

Now that we've successfully extended our newly modified Game Mode to Blueprints, we'll need to set our project to use BP_FPSCharacter as the default Pawn in this step.

  1. In the Edit menu, click on Project Settings.

  2. Under the Project heading on the left side of the Project Settings tab, click on Maps & Modes.

  3. Select BP_FPSCharacter in the Default Pawn dropdown menu.

    SettingFPSCharacter.png

  4. Close the Project Settings menu.

  5. Click the Play button in the Level Editor Toolbar. "We are using FPSCharacter." should now be displayed in red text below "Hello World, this is FPSGameMode!" for five seconds in the upper left corner of the viewport.

    VerifyingFPSCharacterResult.png

    If you're unable to move, you're using FPSCharacter as your Pawn correctly! Your new Character doesn't have any movement controls yet, so you won't be able to move around in the level.

  6. Before going to the next step, press the Escape key or click the Stop button in the Level Editor to exit Play in Editor (PIE) mode.