Unreal Engine 4.x Scripting with C++ Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

The UE_LOG macro accepts a minimum of three parameters:

  • The Log category (we used LogTemp here to denote a log message in a temporary log)
  • The Log level (we used a warning here to denote a log message, printed in yellow warning text)
  • A string for the actual text of the log message itself

Do not forget the TEXT() macro around your log message text, as it will convert the text into a format that is usable by UE_LOGFor those more familiar with coding, the TEXT() macro promotes the enclosed text to Unicode (it prepends an L) when the compiler is set to run with Unicode on. 

UE_LOG also accepts a variable number of arguments, just like printf() from the C programming language:

#include "Chapter_01GameModeBase.h"

void AChapter_01GameModeBase::BeginPlay()
{
Super::BeginPlay();

// Basic UE_LOG message

UE_LOG(LogTemp, Warning, TEXT("Some warning message") );

// UE_LOG message with arguments
int intVar = 5;
float floatVar = 3.7f;
FString fstringVar = "an fstring variable";
UE_LOG(LogTemp, Warning, TEXT("Text, %d %f %s"), intVar, floatVar, *fstringVar );
}

There will be an asterisk * just before the FString variable when using UE_LOG to dereference the FString to a regular C-style TCHAR pointer. This means that it is converting the pointer into the actual value it is pointing at.

TCHAR  is usually defined as a variable type where , if Unicode is being used in the compile, the TCHAR resolves to the built-in data type, wchar_t  . If Unicode is off (the  _UNICODE  compiler switch is not defined), then  TCHAR  resolves to simply the standard char type.
For more information on TCHAR and working with strings in general with C++, check out  https://docs.microsoft.com/en-us/windows/desktop/learnwin32/working-with-strings#tchars.

Don't forget to clear your log messages after you no longer need them from the source! Otherwise, your console may become bloated with messages and make it difficult to find things you are looking for.