アンリアる! C++入門編 ~対話形式で学ぶUnreal Engine~
BOOTHでUnreal Engine C++入門書を販売していますUnreal Engine上でC++を使って開発するために必要となる基礎知識を一冊の本にまとめた本です。 対話形式によるわかりやすい説明を目指しました。無料の試し読みも可能ですので、ぜひ読んでみてください!
[UE5] Use PrintString in C++

The PrintString function is very useful for debugging Blueprint (BP).
It is also useful in C++ implementation.
But for C++ beginner, you may not know how to call it.
This article explains How to call the PrintString function in C++.

How to Call the PrintString function in C++

We can call PrintString function as follows.

  1. Include the UKismetSystemLibrary class
  2. Call the PrintString function

1. Include the UKismetSystemLibrary Class

The PrintString function is defined in the UKismetSystemLibrary class.
Put the following include statement at the top of the source file.

#include "Kismet/KismetSystemLibrary.h"

2. Call the PrintString Function

We will explain how to output a string directly and how to output the value of a variable.

Case: Output a String Directory

We will implment the following process in C++.

PrintString Function

In C++, put the following PrintString function where you want to call it. The third and subsequent arguments can be omitted.

// If not omitted.
UKismetSystemLibrary::PrintString(GEngine->GetWorld(), "Hello", true, true, FLinearColor(0.0f, 0.66f, 1.0f, 1.0f), 2.0f);

// If omitted.
UKismetSystemLibrary::PrintString(GEngine->GetWorld(), "Hello");

The relation between BP and C++ function inputs (arguments) is as follows.

The Relation between BP and C++

Case: Output the Value of a Variable

You may want to output the value of the variable.

PrintString Function

We can implement this by following C++ code. In FString::SanitizeFloat(Val), the float variable Val is converted into a string variable (FString).

UKismetSystemLibrary::PrintString(GEngine->GetWorld(), FString::SanitizeFloat(Val), true, true, FLinearColor(0.0f, 0.66f, 1.0f, 1.0f), 2.0f);

We can convert the other type variable into a string variable as follows (the variable name is assumed to be Val).

Source Data Type How to Convert into a string
float FString::SanitizeFloat(Val)
int FString::FromInt(Val)
FVector Val.ToString()

Conversions from other types are summarized in the official documentation.

3. Sample

We will show an example of the PrintString function.
This example dispalys the result of A+B on the screen.

#include "Hoge.h"
#include "Kismet/KismetSystemLibrary.h"    // Include a header file that UKismetSystemLibrary class is defined.

void UHoge::Func(float A, float B)
{
    float Out = A + B;

    // Call PrintString function.
    UKismetSystemLibrary::PrintString(GEngine->GetWorld(), FString::SanitizeFloat(Out), true, true, FLinearColor(0.0f, 0.66f, 1.0f, 1.0f), 2.0f);
}

How to Research the Calling Process of Functions

(We will publish later in other articles)