Introducing network debugging
Network testing and debugging is an essential skill for any professional working on multiplayer games. It requires a deep understanding of networking protocols and technologies, as well as an ability to identify and diagnose problems quickly. Additionally, it involves troubleshooting both client-side issues on the user’s device and server-side issues on the game’s servers. By mastering this skill, you can ensure that games are running smoothly with minimal latency for all players involved.
As you embark on the development of a networked game, it is imperative to consider the following obstacles that come with creating a seamless and engaging multiplayer experience for your audience, as opposed to a single-player game:
- You will need to debug multiple running instances of the project
- Network communication, by its nature, may be unreliable and unstable, and different clients may have different issues
- A client will work differently as opposed to a server
Unreal Engine comes equipped with a range of dedicated tools and workflows specifically designed for debugging networked applications. By following the guides provided in this chapter, you will gain valuable insights into how to effectively utilize these tools, as well as learn expert tips and best practices for troubleshooting any common networking problems that you may encounter.
Before I delve into how Unreal Engine’s debugging tools operate, it’s essential to have a basic understanding of game debugging.
Explaining game debugging
The process of debugging involves testing every part of the project to make sure everything works as expected and identifying any areas where improvements can be made – this will ensure optimal performance and stability for players when they’re playing your games. Debugging also includes checking the code functionality across different platforms and devices (for example, mobile platforms, desktop, or VR devices), running automated tests on builds before deployment, and so on.
In the end, good debugging practices will allow you to find small details that might otherwise go unnoticed and cause serious problems in development if they’re not addressed early on!
In the previous chapters, you utilized many debug tools provided by Unreal Engine – probably the most used one has been the GEngine->AddOnScreenDebugMessage()
command, which adds the capability of showing messages on the screen.
Some other tools for debugging are purely visuals – such as DrawDebugSphere()
, which you used in Chapter 7, Using Remote Procedure Calls (RPC), to show the position of the Actors your player character can interact with.
If you are familiar with tools such as Microsoft Visual Studio, JetBrains Rider, or any other programming IDE, you will most probably know how important it is to use breakpoints – points in the code where the execution of the code can be temporarily stopped so that you can inspect a program’s data and state.
Debugging a multiplayer game – due to its very specialized nature – needs some more tools to inspect what is happening behind the scenes. In the following subsections, I will introduce you to some of these tools to help you improve your multiplayer programming expertise.
Introducing multiplayer options
Since Chapter 3, Testing the Multiplayer System with a Project Prototype, you have used the most common tool for testing a multiplayer environment by selecting Listen Server Net Mode and choosing the number of players to emulate. These settings are just part of the Multiplayer Options category that can be tweaked in the project settings. To see the full range of options, from the main menu, do the following:
- Select the Window | Editor Preferences option and locate the Level Editor | Play setting.
- Look for the Multiplayer Options category, as shown in Figure 11.1:
Figure 11.1 – The Multiplayer Options category
This category offers a plethora of options for customizing and debugging your game – you have already used Play Net Mode and Play Number of Clients (located in the Client subsection), even if you set these values from another part of the Unreal Editor (that is, the main toolbar). But there’s much more than this!
As an example, you can locate the Multiplayer Viewport Size (in pixels) option and, from the Common Resolutions dropdown, select the client display resolution. This will let you test the look and feel of your game once it’s played on your target devices. Figure 11.2 shows the look of the game once played on a 720x1280 Razer Phone device:
Figure 11.2 – The smartphone display emulation
Hey, I know our project wasn’t originally designed to be played on a smartphone, but you get the idea anyway, right?
Later in this chapter, I will demonstrate some additional features within the Multiplayer Options section – for instance, the traffic emulation settings – that will enhance your proficiency during the debugging phase of your project.
Logging in a networked environment
As you may already know, in Unreal Engine – or any programming environment – logging can be used to debug and track the flow of code at runtime. Logging is a widely used practice in software development, and multiplayer development is no exception. Unreal Engine offers a wide variety of log categories and some of them are dedicated to networking.
The Output Log window keeps track of all the messages and can be opened by clicking on the dedicated button at the bottom of Unreal Engine, as shown in Figure 11.3:
Figure 11.3 – The Output Log activation button
Additionally, all log messages are saved in a .log
file located in your project folder (that is, [Your
Project Folder]/Saved/Logs/
).
Each log message is categorized and can be filtered – as an example, Figure 11.4 shows the log window after I resized the game viewport from the editor:
Figure 11.4 – The log window open
The most common category you will be using while network debugging is LogNet, which includes the majority of network logs.
Note
For an exhaustive list of all the log categories available in a networked environment, check out the official documentation: https://docs.unrealengine.com/5.1/en-US/logging-for-networked-games-in-unreal-engine/.
Filtering the LogNet category
Now that you have some basic knowledge of the network log system, you can try playing the game and checking the Output Log window to see what’s happening under the hood. To do this, follow these steps:
- Open your project in Unreal Engine and click on the Output Log button at the bottom of the editor. Optionally, you can click the Dock in Layout button to dock the window inside the editor and make it non-collapsible.
- Once the log window is open, locate the Filters button and click on it to open all the filters. You will notice that the LogNet category is not shown. To enable it, you need to start a game session – once in Play Mode, you will notice that the category is visible.
- From the Filters list, click on the Categories section and untick the Show All option to deselect all the categories, and then locate the LogNet category to enable it, as shown in Figure 11.5:
Figure 11.5 – The log categories filter
Once you have enabled only the LogNet category, you will get a filtered log list, as depicted in Figure 11.6:
Figure 11.6 – The LogNet output
As you can see, there’s a lot of stuff going on here, and it depends on your game session.
As an extra exercise, take some time and read the log messages. You probably won’t understand much at first sight but, as time goes by, this kind of communication with Unreal Engine will become familiar to you.
In the next subsection, you will learn how to create a log category so that you can easily track what’s happening inside your application.
Creating a custom log category
Usually, when you are caught up in the whirlwind of a project, the temptation is to use log messages without worrying too much about categorizing them. This is a mistake that, of course, will be paid for in the long run. Creating customized categories for your logs is simple, and there are no good reasons not to do it.
To define a custom category, you need to use the DECLARE_LOG_CATEGORY_EXTERN
macro inside a header and, in the corresponding source file, introduce the DEFINE_LOG_CATEGORY
macro. Additionally, the category name must be named with a Log
prefix – for instance, LogMyApp
. As an example, in the next few steps, you will be creating a custom log category for your game, named LogUnrealShadows
, which you can then use anywhere in your project.
So, open your programming IDE and create a new empty class named US_CustomLogs
– you won’t need an Unreal class, just a regular C++ one.
Then, open the US_CustomLogs.h
file and remove the class declaration as you won’t be using it. After that, add the following line of code:
DECLARE_LOG_CATEGORY_EXTERN(LogUnrealShadows, Display, All);
This macro declares LogUnrealShadows
as a new log category to be used in your project. The verbosity for this category is set to Display
; this means that the message will be printed to the console and the log file – if you need to print the message just in the log file and not in the console, you can use the Log
value instead.
Now, open the US_CustomLogs.cpp
file and define the log category by adding the following line of code:
DEFINE_LOG_CATEGORY(LogUnrealShadows);
This macro will let you use the LogUnrealShadows
category anywhere in your project. Once in Play Mode, you will be able to select the LogUnrealShadows
category from the Output Log filter, as shown in Figure 11.7:
Figure 11.7 – The LogUnrealShadows category
Now that the category has been defined, you can use it to add your logs in the game – to do this, you can use the UE_LOG()
macro. As an example, open the US_GameMode.cpp
file and add the needed include
declaration:
#include "US_CustomLogs.h"
Then, to log an alert message inside the AlertMinions()
function class, you must add the following line of code:
UE_LOG(LogUnrealShadows, Display, TEXT("Alerting Minions"));
Figure 11.8 shows the aforementioned message in the Output Log panel once a character has been detected:
Figure 11.8 – The custom message in the Output Log panel
This section introduced you to some of the key debugging tools that are available in Unreal Engine and explained how to effectively utilize them. In the upcoming section, you’ll be presented with how to simulate a networked environment on your personal computer, providing you with the ability to test your project under conditions where major issues may arise during execution.