Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
The Essential Guide to Creating Multiplayer Games with Godot 4.0

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

Product type Book
Published in Dec 2023
Publisher Packt
ISBN-13 9781803232614
Pages 326 pages
Edition 1st Edition
Languages
Author (1):
Henrique Campos Henrique Campos
Profile icon Henrique Campos

Table of Contents (19) Chapters

Preface 1. Part 1:Handshaking and Networking
2. Chapter 1: Setting up a Server 3. Chapter 2: Sending and Receiving Data 4. Chapter 3: Making a Lobby to Gather Players Together 5. Chapter 4: Creating an Online Chat 6. Part 2:Creating Online Multiplayer Mechanics
7. Chapter 5: Making an Online Quiz Game 8. Chapter 6: Building an Online Checkers Game 9. Chapter 7: Developing an Online Pong Game 10. Chapter 8: Creating an Online Co-Op Platformer Prototype 11. Chapter 9: Creating an Online Adventure Prototype 12. Part 3:Optimizing the Online Experience
13. Chapter 10: Debugging and Profiling the Network 14. Chapter 11: Optimizing Data Requests 15. Chapter 12: Implementing Lag Compensation 16. Chapter 13: Caching Data to Decrease Bandwidth 17. Index 18. Other Books You May Enjoy

Caching Data to Decrease Bandwidth

When it comes to reducing bandwidth usage and optimizing network usage in game development, there is a powerful technique that always comes to mind: caching.

Caching solves the question of why we should keep downloading the same data repeatedly when we can download it once, store it somewhere, and reuse it whenever needed. In this chapter, we will delve into caching techniques and learn how to apply them to efficiently download, store, and reuse images and other relevant data. For that, we will use a database that contains image URLs that we are going to download directly from the internet into our players’ machines.

To demonstrate the implementation of these caching techniques, we will prototype a new feature in our game project, where players will have the ability to upload custom images for their spaceships. To save time and focus solely on the network aspect of this feature, we will avoid implementing user experience and user interface...

Technical requirements

It is worth mentioning that this chapter builds upon the concepts presented in Chapter 10, Debugging and Profiling the Network, as well as the project developed in Chapter 9, Creating an Online Adventure Prototype. Therefore, it is crucial to familiarize yourself with the concepts and techniques discussed in those chapters to fully grasp the optimization methods presented here. We will also build upon the final project from Chapter 12, Implementing Lag Compensation, for which you can get the files through the following link:

https://github.com/PacktPublishing/The-Essential-Guide-to-Creating-Multiplayer-Games-with-Godot-4.0/tree/12.prediction-and-interpolation

Moreover, throughout this chapter, it is important to have a basic understanding of how to upload and host content on online services. For instance, we will be downloading files directly from the project’s GitHub repository and from a service called ImgBB, a free image-hosting platform. Without...

Understanding caching

In online multiplayer games, every second counts. Players expect seamless, real-time experiences without interruptions. This is where caching becomes a powerful ally in optimizing game performance. So, what exactly is caching, and why is it crucial for online multiplayer games?

Caching is the process of storing frequently accessed data or resources on a local device or intermediate server. These resources can include images, sound files, 3D models, or even small snippets of code. Instead of fetching these resources from a remote server every time they are needed, the game stores them locally. When a request for these resources arises, the game checks whether it already has a local copy. If it does, it uses the local version, significantly reducing loading times and conserving precious network bandwidth.

The principle behind caching is simple yet effective: if you’ve used something once, it’s likely you’ll need it again. In the context...

Setting up the HTTPRequest node

As mentioned in the chapter’s introduction, we are going to implement a feature that allows players to use custom sprites on their spaceships.

To prototype this feature, we will download images from a third-party image-hosting service that offers free image hosting. We will accomplish this by using a Hypertext Transfer Protocol (HTTP) request to retrieve the image file from the third-party servers. Let’s delve into the workings of HTTP to fully understand how it operates and grasp the implementation process.

Understanding the HTTP protocol

HTTP serves as the foundation for communication on the World Wide Web. It is a protocol that defines the interaction and data exchange between clients and servers. Invented by Tim Berners-Lee in the early 1990s, HTTP was initially designed to facilitate the retrieval of hypertext documents, commonly known as web pages. Over time, it has evolved to support various types of content, including images...

Implementing texture caching

In the previous section, we introduced the HTTPRequest node, a built-in solution offered by Godot Engine to make HTTP requests. Then we created TextureDownloadHTTPRequest, which is a custom node specialized in downloading images from our PlayersSpaceship.json database. Now, let’s dive deeper into integrating this node into our Player2D class so we can actually use it in our prototype.

In this section, we will create a method to enable the server to change a player’s spaceship sprite dynamically. But we won’t just load any sprite; we’ll fetch the proper file from the user://.cache/ folder we set up in the Setting up the scenes and database section. This approach will enhance the customization and interaction in your game, allowing the server to deliver custom sprites to players in real time.

To achieve this, we’ll create a method called load_spaceship(). This method will play a fundamental role in our implementation...

Implementing database caching

With everything in place, it is time to go one step above and work on the World scene that is going to glue everything together and run the proper procedures to ensure that all players will see the same custom sprites. We do that in this node because this is the class responsible for setting up everything related to world synchronization, which includes the players’ spaceship custom sprites. We will need to make some changes in some core methods to achieve that, but this is for the better.

Since we are working with a prototype, there’s no fear of altering function signatures and other core aspects of a class. But note that if this was production-ready, we would call this class “closed” and avoid making core changes like the ones we are about to make. This would keep our game code base consistent and avoid errors. Though the changes we are about to make will mostly extend the class functionalities, we will add an argument...

Going further with caching

Well, as we saw, images are not the only type of data we can download through the internet using the HTTPRequest node; for instance, we also downloaded the PlayersSpaceship.json file, which is a text file. But we can download pretty much anything using this protocol, provided it is stored in an HTTP page. But sometimes, some files are not stored and made available publicly on an HTTP page that any browser can access. Usually in this type of situation, the backend engineer will create a REST API that we can use to retrieve these files directly from the database where they are stored.

This type of feature demands a physical infrastructure and the development of a custom REST API so we can work with it. Unfortunately, this goes way beyond the scope of this book. But the whole idea is that you can perform an HTTP request using custom headers and a custom URL that resembles an RPC. So, in the very URL itself you would add some parameters that the REST API would...

Summary

Well, it’s time to wrap this chapter up! In this chapter, we discussed what caching is and how it allows us to save bandwidth. We learned how to implement it for custom spaceship sprites in our game. For that, we saw how we can use the HTTPRequest node to download files over the internet by making HTTP requests. We also implemented a custom Monitor to see how much data we saved throughout a play session by caching textures. Finally, we saw how caching can go beyond image and text files and the possibility of using REST APIs to download all sorts of files using HTTP.

With that, my fellow Godot Engine developers, we reach the end of our journey!

We started as people who didn’t know how to send a simple message to another computer, and became fully fledged network engineers ready to create the games of our dreams and allow players to have a shared experience, enjoy their time together, and build communities around the world. Congratulations on completing this...

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 2023 Publisher: Packt ISBN-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.
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}