Language:
Page Info
Skill Level:
Engine Version:

1.3 - Adding Log Messaging

Choose your OS:

A great way to start adding code to our project is by adding a log message to FPSGameMode. Log messages are really useful for verifying and debugging code during development. During this step, we'll use a log message to verify that we're actually using FPSGameMode rather than the default Game Mode provided to us by Unreal Engine.

FPSProjectGameMode Header File

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

    InterfaceFile.png

  2. Double-click FPSProjectGameMode.h to open the header file for your FPSGameMode class.

  3. Your class declaration should look like the following:

    UCLASS()
    class FPSPROJECT_API AFPSGameMode : public AGameModeBase
    {
        GENERATED_BODY()
    };
  4. Add the following function declaration under the AFPSProjectGameMode constructor declaration:

    virtual void StartPlay() override;

    This function declaration allows you to override the StartPlay() function (inherited from the AActor class) so that you can print a log message to the screen when gameplay begins.

  5. FPSProjectGameMode.h should now look like the following:

    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "GameFramework/GameModeBase.h"
    #include "FPSProjectGameMode.generated.h"
    
    /**
     * 
     */
    UCLASS()
    class FPSPROJECT_API AFPSProjectGameMode : public AGameModeBase
    {
        GENERATED_BODY()
    
        virtual void StartPlay() override;
    };
  6. Save your header file in Visual Studio.

FPSProjectGameMode CPP File

  1. In the Solution Explorer, locate FPSProjectGameMode.cpp (FPSProject > Source > FPSProject).

    ImplementationFile.png

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

  3. Now add the following lines of code for the StartPlay() function below the AFPSProjectGameMode constructor:

    void AFPSProjectGameMode::StartPlay()
    {
        Super::StartPlay();
    
        if (GEngine)
        {
            // Display 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::Yellow, TEXT("Hello World, this is FPSGameMode!"));
        }
    }

    StartPlay() will now print a new debug message ("Hello World, this is FPSGameMode!") to the screen in yellow text for five seconds when gameplay begins.

  4. FPSProjectGameMode.cpp should now look like the following:

    // Fill out your copyright notice in the Description page of Project Settings.
    
    #include "FPSProject.h"
    #include "FPSProjectGameMode.h"
    
    void AFPSProjectGameMode::StartPlay()
    {
        Super::StartPlay();
    
        if (GEngine)
        {
            // Display 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::Yellow, TEXT("Hello World, this is FPSGameMode!"));
        }
    }
  5. Save your CPP file in Visual Studio.