There are many reasons why this may be needed: for example, for debugging or testing code execution.
How to print messages to console
We can define a custom log:
DECLARE_LOG_CATEGORY_EXTERN(LogNameExample, Log, All);
If you decide to use a custom log, just replace "LogTemp" from the examples below with the name of your log ("LogNameExample" from the example above)
Simple message:
UE_LOG(LogTemp, Warning, TEXT("Message example"));
String message:
FString StringExample = TEXT("String example");
UE_LOG(LogTemp, Warning, TEXT("Output: %s"), *StringExample);
Int message:
int32 IntegerExample = 4; // you can use int8, uint8, int16, uint16, int32, uint32, int64, uint64 to log this way
UE_LOG(LogTemp, Warning, TEXT("Output: %d"), IntegerExample);
Float message:
float FloatExample = 10.4;
UE_LOG(LogTemp, Warning, TEXT("Output: %f"), FloatExample);
FVector message:
FVector VectorExample = FVector(100, 200, 300);
UE_LOG(LogTemp, Warning, TEXT("Output: %s"), *VectorExample.ToString());
FName message:
FName NameExample = FName("Name example");
UE_LOG(LogTemp, Warning, TEXT("Output: %s"), *NameExample.ToString());
FText message:
FText TextExample = FText::FromString("Text example");
UE_LOG(LogTemp, Warning, TEXT("Output: %s"), *TextExample.ToString());
How to print messages to screen
For everything to work well, you need to include "EngineGlobals.h" in the declaration (.h) file:
#include "Runtime/Engine/Public/EngineGlobals.h"
Simple message:
GEngine->AddOnScreenDebugMessage(-1, 12.f, FColor::White, TEXT("Simple message"));
Custom message (int, float, FVector, etc):
int32 IntegerExample = 4; // you can use int8, uint8, int16, uint16, int32, uint32, int64, uint64 to log this way
float FloatExample = 10.4;
FVector VectorExample = FVector(100, 200, 300);
GEngine->AddOnScreenDebugMessage(-1, 12.f, FColor::White, FString::Printf(TEXT("Output: %d %f %s"), IntegerExample, FloatExample, *VectorExample.ToString()));
Much more detailed information is available on the UE4 Community Wiki page