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

Creating an Online Chat

Welcome to the next chapter of our book on making online multiplayer games using Godot Engine 4.0!

In the previous chapter, we saw how to make a lobby for players and its importance to gathering before entering a game. Now, we will take a step further and explore the development of an online chat using Godot’s Network API.

As we saw, Godot’s Network API provides us with a robust set of tools for building real-time multiplayer games. In this chapter, we will use the ENetMultiplayerPeer class to establish a reliable connection between players, and some remote procedure call (RPC) methods to handle the chat system’s logic.

As you may already know, a chat system is essential in any online multiplayer game, as it enables players to communicate with each other during gameplay. A well-designed chat system can greatly enhance the player experience, allowing for smoother coordination between team members and encouraging socialization among...

Technical requirements

Through this chapter, we are going to use the fourth folder of our Godot Engine project repository, available through the following link:

https://github.com/PacktPublishing/The-Essential-Guide-to-Creating-Multiplayer-Games-with-Godot-4.0

After importing the project into your Godot Engine project manager, open it and navigate to the res://04.creating-an-online-chat folder. Then, open the ChatControl.tscn and ChatControl.gd files. They are going to be the focus of this chapter.

Coming next in this chapter, we are going to learn the basic concepts of reliable and unreliable data exchange and how channels work so we have the ground set for our chat system.

Understanding data exchange and channels

A chat system is essentially a chronological stack of messages that we order based on what players send to each other. Since this system needs a coherent chronological order, we need to understand how to prevent data from getting mixed and disorganized as it gets transmitted throughout the network. We also need to prevent this ordering from impacting other systems and components. To do that, we are going to learn how we can send packets reliably and how we can use multiple channels for data exchange.

Reliable and unreliable packets

Godot Engine’s Network API allows for reliable and unreliable data exchange between peers. The @rpc annotation provides a way to transmit data securely between clients and the server using different transport protocols such as UDP and TCP. Reliable data exchange ensures that data is delivered in order and is not lost in transit, making it ideal for crucial data such as chat messages. Unreliable data exchange...

Sending chat messages

Godot Engine’s RPCs allow for efficient data transmission between clients and the server in multiplayer games. We can create an RPC method for this specific purpose, with message data as arguments. The transmission can be either reliable or unreliable, depending on the needs of the application. Once the message is sent, it’s received by the appropriate recipients (including clients and the server) who handle it appropriately, such as by displaying it to the user or logging it. We did that in the Understanding data exchange and channels section when we made the add_message() method.

Sending messages using Godot’s RPCs is a straightforward process that involves defining the message format. In our case, we use the player’s avatar name and the message content, as seen previously, calling an RPC method to transmit the message and handling the message appropriately on the receiving end.

We are going to implement a method to read the...

Updating peer’s data remotely

Something really cool about Godot Engine’s Network API is that we can abuse RPCs to pass data around. For instance, we’ve seen that we use the player’s avatar name in our messages. But have you asked yourself how we retrieve this data in any of these steps?

You probably saw that there’s an RPC method called set_avatar_name(), right? Since its @rpc annotation doesn’t have any options, you can assume that it uses the default options. This is important to know because, as we saw previously, this means that it should be called remotely only by the Multiplayer Authority – in this case, the server.

Let’s open ChatServer.gd to understand what’s happening behind the scenes. In essence, most of it is pretty much the same as in the Lobby project, but you will notice something slightly different in the retrieve_avatar() RPC method. In line 39, we have the following instruction:

var peer_id =...

Summary

Throughout this chapter, we saw how we can use RPC methods to pass data around and perform actions on multiple peers of our network. We also understood the core difference between reliable and unreliable data exchange and saw some examples of situations of when to use each one. Due to this core difference in the way we can exchange data between the peers of our network, we also understood that one way may block the other, so we can use channels to prevent that one type of data from getting in the way of another type of data unrelated to that exchange.

By creating an online lobby where players can chat, we saw how to use the @rpc annotation with some of its available options, including the option to allow other peers to make remote calls instead of only the Multiplayer Authority.

In the next chapter, we will use the knowledge we’ve just acquired to build an actual real-time multiplayer experience. We’ll create a multiplayer online quiz where players will...

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 €14.99/month. Cancel anytime}