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

Sending and Receiving Data

In the previous chapter, we saw how we can establish a connection between two computers using the high-level Godot Engine ENetMultiplayerPeer API. But what do we do after that? Why do we establish connections between computers? The foundation of a network is the communication between the connected computers, allowing them to send and receive data. This data is transferred by breaking down the content into small chunks called packets.

Each packet is like a postcard containing the necessary information, such as the sender’s and receiver’s IP addresses, the communication port, and the message’s content. We then send these packets over the network, where they can be routed to their intended recipient. Using communication protocols, such as the UDP protocol, we break the data into packets at the sending end and reassemble them at the receiving end of the relationship.

In this chapter, we will discuss the fundamentals of how packets are...

Technical requirements

In this chapter, we’re going to keep up with our project in Godot Engine, but this time, we are going to use the files provided in the res://02.sending-and-receiving-data folder. So, if you haven’t already done so, download the project’s repository using this link: https://github.com/PacktPublishing/The-Essential-Guide-to-Creating-Multiplayer-Games-with-Godot-4.0.

Then, with the project added to your Godot Engine project manager, open the project and proceed to the res://02.sending-and-receiving-data folder.

Understanding packets

Packets are fundamental building blocks of communication over the network using the UDP protocol. They are small chunks of data that contain all the necessary information to reach their intended recipient. This includes the sender’s and receiver’s IP addresses, the communication port, and the message’s content.

Senders send packets to the receiver over the network. The receiving end reassembles the packets, allowing the receiver to understand the message sent. This process is known as packet switching. You can see a visual representation of this here:

Figure 2.1 – Packet switching process

Figure 2.1 – Packet switching process

Unlike other protocols, such as the TCP protocol, the UDP protocol does not guarantee that packets will arrive in the same order as they were sent. This means that the protocol is less reliable but more efficient and faster.

UDP is also different from other protocols due to its lack of connection state. Each packet...

Introduction to the JSON format

In network programming, transmitting objects directly through the network is not always reliable, as the data may get corrupted or lost in transit. Moreover, transmitting objects containing executable code may pose a security risk if the code is malicious. That’s why it’s a common practice to use data serialization to convert objects into a format that can be easily transmitted over the network.

One of the most commonly used data serialization formats is JSON. JSON is a lightweight, text-based format that can represent complex data structures such as arrays and objects, making it an ideal choice for network communication.

When using the Godot Engine network API with UDP, sending and receiving JSON files is a common practice. With JSON, we can serialize and deserialize data quickly and efficiently. JSON files are human-readable, making it easier for developers to debug and troubleshoot issues. JSON files are also flexible, meaning...

Sending packets with PacketPeerUDP

Now, let’s move on to practical knowledge. In this chapter, your task is to implement a login system for a game. Our project already has a cool user interface and is able to gather player data, such as their login name and password. Your mission is to make sure that only authorized players can access the game’s content by implementing a secure authentication feature.

Once a player successfully logs in, you need to display their character’s avatar based on what we have saved in our database. As a network engineer, you understand the importance of security when it comes to online systems. You know that a robust authentication system is essential to ensure that only legitimate users are granted access to the game’s content.

Therefore, you will need to develop a login system that checks players’ credentials against a secure database and verifies if they have permission to access the game’s features or not...

Listening to packets with UDPServer

Welcome to our Godot Engine server scene! This scene is where our game’s server logic is implemented.

The server is the backbone of our game, responsible for authenticating players and providing them with data about their avatars, such as their name and texture file. This node is called Server, and it has a pre-written script that includes some essential variables. Among them are two vital variables: database_file_path and logged_users.

The database_file_path variable is the path to the FakeDatabase JSON file, which represents a fake database that holds the players’ data. The logged_users variable is a dictionary that stores players who are currently logged in.

These variables are crucial to our server’s functionality, and we will use them to authenticate players and provide them with the data they need.

Let’s implement the Server node’s most important feature, which is to listen to packets. Proceed...

Authenticating the player

Authenticating player credentials is a crucial aspect of any multiplayer game. In our project, we are building a login system for a game using Godot Engine. The login system allows players to log in with their username and password and then displays their character’s avatar upon successful login.

We are going to use a fake database, stored as a JSON file, to represent the players’ credentials. While this approach is simpler than using a full-fledged database management system, it has its own security risks. So, be aware of the risks of this approach in a production-ready project.

To authenticate player credentials in our project, we will also use Godot’s FileAccess class to load the fake database from the JSON file and parse the data. This will allow us to compare players’ login credentials with the data in the database and authenticate the player if the credentials match.

Loading a fake database

Now, let’s load...

Loading the player’s avatar

Welcome to AvatarScreen! This is where the player will be able to customize their avatar appearance and select a unique name in the final version of our (fake) game. To display their current available avatar, we need to load the player’s avatar data from the database and display it on the screen.

For that, the AvatarScreen scene is made up of a Control node called AvatarScreen, which holds all the other nodes in the scene, including a Control node called AvatarCard:

Figure 2.5 – The AvatarScreen scene’s node hierarchy

Figure 2.5 – The AvatarScreen scene’s node hierarchy

The AvatarCard node contains a TextureRect node to display the avatar’s image using a texture file and a Label node to display the avatar’s name.

To load the player’s avatar, we first need to retrieve the path to the image file from our fake database, which we previously populated with avatar information. So, before we dive into the action in the AvatarScreen...

Summary

In this chapter, we saw how we can establish a connection between server and client using the UDP protocol implementation in Godot Engine’s network API. With that, the network peers can open a communication channel and exchange data.

Since this implementation works on quite a low-level approach, we saw how we can create a simple API for our peers to make, understand, and reply to each other’s requests. Depending on the request, it might be necessary to follow a process known as serialization, which is how we take relevant information from our game state and turn it into a format that we can store and pass around. In our case, we saw that JSON format is one of the most common serialization formats.

Using the JSON format, we saw how we can parse our Godot Engine string as JSON and also how to turn a JSON file into a dictionary that we can work with more efficiently using GDScript.

At the end of the chapter, we saw how we can authenticate players’...

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