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

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 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