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.
- Include the UKismetSystemLibrary class
- 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++.
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.
Case: Output the Value of a Variable
You may want to output the value of the variable.
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)