Reader small image

You're reading from  The Essential Guide to Creating Multiplayer Games with Godot 4.0

Product typeBook
Published inDec 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803232614
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Henrique Campos
Henrique Campos
author image
Henrique Campos

Henrique Campos is an indie game developer and game designer working in the industry since 2015. Starting as a University teacher in the Computer Graphics and Artificial Intelligence chairs and working for the GDQuest team from 2018 to 2022, he has also been providing consultancy for solo developers, studios, and schools. Under the alias of Pigdev, Henrique has been creating game development content on his YouTube channel since 2016. Among his projects, he wrote the Top 7 Godot Engine Recipes and the Platformer Essential Recipes ebooks where he presents design patterns that people can use to make games with the Godot Engine. A passionate open-source enthusiast, Henrique has been working and contributing to the Godot Engine project since 2016.
Read more about Henrique Campos

Right arrow

Optimizing Data Requests

Welcome to Chapter 11, Optimizing Data Requests, where we will use the tools that we saw in Chapter 10, Debugging and Profiling the Network, and finally implement improvements to the network code that we wrote in Chapter 9, Creating an Online Adventure Prototype.

In this chapter, we will understand a bit more about bandwidth and throughput by analyzing the current state of our game. We saw, in the Using the network profiler section of Chapter 10, Debugging and Profiling the Network, that we have some improvements to make, especially regarding the MultiplayerSynchronizers and the QuestDatabase data transmission. So, here, we will see how we can decrease the number of requests and how we can compress and decompress data to reduce the bandwidth and throughput and make our game available to more people in a more reliable and optimal way.

By the end of this chapter, you will understand that there are many ways to optimize a game and most of the optimizations...

Technical requirements

As mentioned in Chapter 10, Debugging and Profiling the Network, Part 3, Optimizing the Online Experience, is based on the final version of the project made in Chapter 9, Creating an Online Adventure Prototype, so it’s fundamental to have read, practiced, and implemented the concepts presented there.

You can get the files necessary to get started with this chapter through the following link. These files have the implementations we’ve made in Chapter 10, Debugging and Profiling the Network:

https://github.com/PacktPublishing/The-Essential-Guide-to-Creating-Multiplayer-Games-with-Godot-4.0/tree/11.optimizing-data-requests

It’s also necessary that you have read and understood the concepts and tools presented in Chapter 10, Debugging and Profiling the Network, so that we can move forward under the assumption that you already know what they are and how to use them properly. In particular, you will need to understand how the Debugger&...

Understanding network resources

We’ve already mentioned the importance of bandwidth and throughput; in Chapter 1, Setting up a Server, we even had a brief introduction to and a visual representation of the topic in Figure 1.3 and Figure 1.4. Now it’s time to wrap our heads around these concepts, which are fundamental to network usage optimization and will be our major resources to measure the improvements we made toward our optimization goals.

As a general rule, the less bandwidth and the lower the throughput of our network code, the better. Of course, we need to keep in mind that all optimizations should maintain the game experience, so we are in a very delicate position. Different from other processing, memory, and graphics optimizations, our work can’t create “beautiful accidents,” such as a processing optimization that can lead to a cool mechanic. No, our job as network engineers is to replicate the already established mechanics and effects...

Decreasing the requests count

Previously, in Chapter 10, Debugging and Profiling the Network, we saw that there was a disproportional and unnecessary number of requests being made to the Weapon2D node’s fire() RPC method, and we even came up with what could be a solution for this issue. We also saw that we can decrease the Asteroid node’s sync problem by only updating it once a given player requests a synchronization using the World node’s sync_world() RPC method.

In this section, we are going to implement these optimization methods and improve the overall performance of our network. Let’s start with the Weapon2D node issue.

Reducing the weapon fire count

Sometimes we may need to make changes in the core code of a feature in order to improve its network performance, even when its local performance stays the same, or may even drop. When talking about optimization, we are always trying to balance things out and figure out how to use the available...

Compressing data with the ENetConnection class

The Godot Engine’s high-level network API includes the ENetConnection class. This class is available after a connection between peers, such as the server and client, is established. Using ENetConnection, we can tweak the peer’s connections, allowing us to tell, for instance, the compression method. We can access the ENetConnection instance after the peers have successfully connected. For that, we can use the ENetMultiplayerPeer.host property.

There are five available compression methods to use in the ENetConnection class:

  • None, which can be used through the CompressionMode enumerator’s COMPRESS_NONE option. This is what the documentation says:

    This uses the most bandwidth but has the upside of requiring the fewest CPU resources. This option may also be used to make network debugging using tools like Wireshark easier.

  • ENet’s built-in Range Coder, which can be used through the CompressionMode enumerator...

Summary

In this chapter, we learned how to optimize data requests in our game to improve network performance. We focused on decreasing the number of requests being made in the game by optimizing the firing of the weapon. We saw how to create an RPC method that can change the weapon’s firing state and how to toggle the firing state based on the “shoot” action being pressed or released instead of triggering the firing on every frame in the _process() callback. Finally, we saw how to use the network profiler to assess the impact of these optimizations on the game’s performance.

After that, we tackled the issue of Asteroid node’s MultiplayerSynchronizers’ syncs. We disabled the automatic synchronization by setting the Visibility Update Node of MultiplayerSynchronizer to None and manually synced by using the MultiplayerSynchronizer.update_visibility() method once the player joins the game. This decreased the Asteroid node’s sync count...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Essential Guide to Creating Multiplayer Games with Godot 4.0
Published in: Dec 2023Publisher: PacktISBN-13: 9781803232614
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Henrique Campos

Henrique Campos is an indie game developer and game designer working in the industry since 2015. Starting as a University teacher in the Computer Graphics and Artificial Intelligence chairs and working for the GDQuest team from 2018 to 2022, he has also been providing consultancy for solo developers, studios, and schools. Under the alias of Pigdev, Henrique has been creating game development content on his YouTube channel since 2016. Among his projects, he wrote the Top 7 Godot Engine Recipes and the Platformer Essential Recipes ebooks where he presents design patterns that people can use to make games with the Godot Engine. A passionate open-source enthusiast, Henrique has been working and contributing to the Godot Engine project since 2016.
Read more about Henrique Campos