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

Preface

The Essential Guide to Creating Multiplayer Games with Godot 4.0 is the ultimate hands-on guide to understanding how to make online multiplayer games using the open source Godot Engine. In its fourth version, Godot Engine introduces a high-level network API that allows users to focus on creating interesting and funny mechanics while letting the engine do the heavy work.

Through this book, you will learn the fundamentals of networks, including the basic UDP, TCP, and HTTP protocols. You will see how Godot Engine uses its ENet library implementation to seamlessly integrate these protocols into its game development workflow. Through nine projects, including five games, one of them an online multiplayer adventure game, you will learn all you need to know to connect players together in amazing shared experiences.

Who this book is for

This book is made for intermediary Godot Engine users, people who already know how Godot Engine works, its design philosophy, the editor, the documentation, and its core features. These users have already made games using Godot Engine. They are looking for something to make their next project stand out, and adding online multiplayer features is that thing.

What this book covers

Chapter 1, Setting up a Server, explains what a network is and how Godot Engine implements networking features through its ENet library implementation. You’ll learn how to make your first handshake to effectively connect a server and a client machine.

Chapter 2, Sending and Receiving Data, discusses the foundation of a network, which is for multiple computers to exchange data with each other. In this chapter, you will learn that we use a data structure called packets, and we serialize data to recreate game states on the network. For this, we use the UDP protocol. At the end, we will have a login screen, effectively retrieving data from the server.

chapter 3, Making a Lobby to Gather Players Together, explains how Godot Engine eases the process of serializing and exchanging data using the industry-standard UDP protocol by providing Remote Procedure Calls (RPCs), allowing us to essentially make calls to methods on remote objects. By the end of the chapter, we expand the login screen into a lobby, adding another client to the equation and connecting three computers together.

Chapter 4, Creating an Online Chat, explains that with the power of RPCs, we can now change objects’ states remotely with ease. In this chapter, we learn how we use RPCs to allow players to exchange messages in a chat. We discuss how we can use RPCs and channels to prevent network bottlenecks. With that, we prepare ourselves to implement actual game features. By the end of the chapter, we will have a fully functional chat.

Chapter 5, Making an Online Quiz Game, explains how we can synchronize players’ game states based on the general game state. We will set up a server that will react to player interactions and change the game state from waiting for a response to processing a match winner, announcing the winner, starting a new match, and reaching the end of available quiz questions, effectively ending the game. By the end of the chapter, we will have an online quiz game where multiple players compete to answer the most questions correctly.

Chapter 6, Building an Online Checkers Game, moves on to implementing a turn-based multiplayer online game, and nothing is better than the classic checkers for that. In this chapter, we will learn how to get the most out of our RPCs while still maintaining the heavy processing on players’ machines. We will also discuss MultiplayerSynchronizer, a node that allows us to easily synchronize nodes’ properties remotely. We will also learn what Multiplayer Authority is, which prevents players from messing around with other players’ objects. By the end of the chapter, we will have a fully functional online checkers game.

Chapter 7, Developing an Online Pong Game, begins the transition from turn-based to action. Action games rely heavily on players’ reaction times, and the game world should update its state quickly to allow players to have a smooth experience. Here, we will develop an online Pong game and use the MultiplayerSynchronizer node to sync the players’ paddles and the ball. We will also learn that some features should use different syncing processes. We will go even deeper into the Multiplayer Authority realm to prevent one player’s input from interfering with the other player’s paddle’s movement. By the end of the chapter, we have a playable multiplayer online Pong game.

Chapter 8, Designing an Online Co-Op Platformer, is where our baby steps stop and we start to implement interesting features for a custom game. We will prototype a physics puzzle platformer game where players grab and move crates around to overcome obstacles and reach the level’s goal. Applying everything we’ve learned in the previous chapters, we will expand the usage of the MultiplayerSynchronizer by syncing animations on top of objects’ positions. By the end of the chapter, we will have a working prototype of a physics puzzle co-op platformer game.

Chapter 9, Creating an Online Adventure Prototype, is, if we’re honest, what you have been looking for throughout this book. Here, we will use all our skills to create an online multiplayer adventure game. Players can join and leave at any time and the world is persistent, maintaining players’ progress in quests. We will discuss the basics of making an MMORPG game, storing and retrieving players’ quest progress in a database, syncing players’ spaceships and bullets, and making their actions affect other players’ experiences. By the end of the chapter, we will have a prototype of an online multiplayer top-down space-shooter adventure game.

Chapter 10, Debugging and Profiling the Network, moves on from implementing the online multiplayer features on our top-down space-shooting adventure prototype. We now need to pave the way for thousands of players to play our game simultaneously. For that, we will use Godot Engine’s built-in debugging and profiling tools to assess the potential areas for improvements in our game. We will focus on the network profiler and the monitor debugging tools to assess and propose potential solutions for the bottlenecks we find in our prototype. By the end of the chapter, we will have two of the most powerful and necessary skills a developer can have: the ability to debug and optimize a game.

Chapter 11, Optimizing Data Requests, builds on the understanding of the tools we have at our disposal to assess the information we need to discover potential areas for improvement; now, it’s time to get our hands dirty. Throughout this chapter, we will learn how to create custom monitors to gather data about specific game features and decide the best strategy to optimize them. By the end of the chapter, we will have refactored our top-down space-shooting adventure, decreasing the bandwidth and the number of RPCs we make, effectively making our network consumption much lighter. We will have also implemented several techniques to decrease the network load, assessing each improvement with the network profiler and custom monitors to see how much better the game is becoming.

Chapter 12, Implementing Lag Compensation, deals with the issue that due to the improvements made to decrease network usage, our game may be inaccurately replicated on players’ machines. With fewer RPCs and more sparse synchronization, the game may become asynchronous among players. Add latency and packet loss on top of that and you effectively worsen the players’ experience. Nobody likes lag in their game. In this chapter, we will learn how to use Tweens to implement interpolation, prediction, and extrapolation to compensate for all these issues. By the end of the chapter, we will have a version of the top-down space-shooting adventure prototype with some fake latency and solutions for this game-breaking issue.

Chapter 13, Caching Data to Decrease Bandwidth, handles an important issue: throughout our network engineering endeavors, we have learned that bandwidth is our core resource and that we should always look to optimize its usage. In this chapter, we will learn how to use HTTP to download some data and store it on players’ machines so that we can reuse it when necessary. By the end of the chapter, we will have implemented a feature that allows players to use custom images for their spaceships, and this new image will be replicated on all other players’ instances of the game. To save bandwidth, we will implement caching using the user data folder.

To get the most out of this book

This book uses Godot Engine’s ENet library implementation to create several prototypes that explore the boundaries of this technology. To get the most out of this book, you need to understand how Godot Engine works, how to code in GDScript, how to use Git, and the basics of the UDP, TCP, and HTTP protocols.

Software/hardware covered in the book

Operating system requirements

Godot Engine 4.0

Windows, macOS, or Linux

Throughout the book, we use the projects available in the book’s GitHub repository. This book focuses on the networking aspects of each project, so to save you time, you have ready-to-use projects available so you don’t need to bother with implementing other features that are not related to network engineering. In that sense, having Git installed is recommended, although not mandatory because you can download the code directly through the links provided as well.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/The-Essential-Guide-to-Creating-Multiplayer-Games-with-Godot-4.0/. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “One of the Godot Engine’s Network API core features is the ENetMultiplayerPeer class.”

A block of code is set as follows:

func _process(delta):
    server.poll()
    if server.is_connection_available():
        var peer = server.take_connection()
        var message = JSON.parse_string(peer.get_var())
        if "authenticate_credentials" in message:
            authenticate_player(peer, message)
        elif "get_authentication_token" in message:
            get_authentication_token(peer, message)

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “From there, we first need a server. So, choose one instance and press the ServerButton.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read The Essential Guide to Creating Multiplayer Games with Godot 4.0, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781803232614

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
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 €14.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