Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unreal Engine 4.x Scripting with C++ Cookbook

You're reading from   Unreal Engine 4.x Scripting with C++ Cookbook Develop quality game components and solve scripting problems with the power of C++ and UE4

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789809503
Length 708 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
John P. Doran John P. Doran
Author Profile Icon John P. Doran
John P. Doran
Stephen Whittle Stephen Whittle
Author Profile Icon Stephen Whittle
Stephen Whittle
William Sherif William Sherif
Author Profile Icon William Sherif
William Sherif
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. UE4 Development Tools FREE CHAPTER 2. Creating Classes 3. Memory Management, Smart Pointers, and Debugging 4. Actors and Components 5. Handling Events and Delegates 6. Input and Collision 7. Communication Between Classes and Interfaces: Part I 8. Communication Between Classes and Interfaces: Part II 9. Integrating C++ and the Unreal Editor: Part I 10. Integrating C++ and the Unreal Editor: Part II 11. Working with UE4 APIs 12. Multiplayer Networking in UE4 13. AI for Controlling NPCs 14. User Interfaces - UI and UMG 15. Other Books You May Enjoy

UE4 – logging with UE_LOG

Logging is extremely important for outputting internal game data. Using log tools lets you print information into a handy little Output Log window in the UE4 editor.

Getting ready

When coding, we may sometimes want to send some debug information out to the UE log window. This is possible using the UE_LOG macro. A macro is a fragment of code that has been given a name. Whenever the name is used in code, it is replaced by the contents of the macro at compile time. Log messages are an extremely important and convenient way to keep track of information in your program as you are developing it.

You should have a code file to complete this recipe. If this is your first time coding in Unreal, you should complete the previous recipe before continuing with this one.

How to do it...

  1. In your code, enter a line of code using the following form:
UE_LOG(LogTemp, Warning, TEXT("Some warning message") );

For instance, if you wanted to add this to the script in the previous recipe, it may look like this:

#include "Chapter_01GameModeBase.h"

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

// Basic UE_LOG message
UE_LOG(LogTemp, Warning, TEXT("Some warning message") );
}
  1. Turn on the Output Log inside the UE4 editor by going to Window | Developer Tools | Output Log to see your log messages printed in that window as your program is running:
  1. If you play your game by clicking on the Play button from the top toolbar, you should notice our text being displayed in yellow on the log:
To make your output easier to see, you can clear the Output Log at any time by right-clicking on it within the window and selecting Clear Log.

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_LOG. For 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.

You have been reading a chapter from
Unreal Engine 4.x Scripting with C++ Cookbook - Second Edition
Published in: Mar 2019
Publisher:
ISBN-13: 9781789809503
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image