ue4 c++ print to screen

UE4 C++ Print to Screen

// Include the necessary header file
#include "Engine.h"

// Inside a function or method
if (GEngine)
{
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Hello, world!"));
}

Explanation:

  • Include the necessary header file: This line includes the "Engine.h" header file, which provides access to the Unreal Engine's functionality for printing messages to the screen.

  • Inside a function or method: The code for printing to the screen should be placed inside a function or method within a class.

  • Check if GEngine is valid: The if (GEngine) statement checks if the GEngine global pointer is valid before attempting to use it.

  • Use GEngine to add on-screen debug message: The GEngine->AddOnScreenDebugMessage function is used to display a message on the screen. It takes parameters for message index, display time, color, and the message text itself.

  • Parameters:

  • -1: Message index, -1 means it will create a new message.
  • 5.f: Display time in seconds, in this case, 5 seconds.
  • FColor::Red: Color of the message, in this case, red.
  • TEXT("Hello, world!"): The text message to be displayed on the screen, in this case, "Hello, world!" wrapped with TEXT macro for string literal conversion.

This code will display the message "Hello, world!" in red color on the screen for 5 seconds when executed.