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

Making a Lobby to Gather Players Together

In the previous chapter, we discussed how to use UDP packets to exchange data between multiple players in a game. While this approach is highly efficient, it requires a lot of manual work to ensure that data is sent and received correctly. In this chapter, we will explore the high-level network API of the Godot Engine, which simplifies the networking process by providing a set of built-in tools and functions that can handle common network tasks.

Specifically, we will focus on the ENetMultiplayerPeer API, which is the Godot Engine’s wrapper class for its ENet library implementation, and Remote Procedure Call (RPC), a communication protocol that allows us to make calls to functions and methods on remote computers as if we were making them locally. We will use these tools to create a lobby, authenticate players, retrieve player avatar data from a fake JSON database, and synchronize all players when a player enters the lobby. We will...

Technical requirements

In this chapter, we will build yet another project using the Godot Engine. Remember, throughout this book, we are using Godot Engine version 4.0, so this is also a requirement.

This time around, we are going to use the files provided in the res://03.making-lobby-to-gather-players folder. So, if you don’t have the project repository yet, download it through 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’s project manager, open the project and proceed to the res://03.making-lobby-to-gather-players folder.

Calling functions remotely with RPCs

In a network context, RPC stands for remote procedure call, which is a protocol that allows one program to call a function or procedure on another program running on a different machine or over a network. In the context of the Godot Engine, RPCs allow objects to exchange data between each other over the network, which is a key feature in creating multiplayer games.

To use RPCs in the Godot Engine, we need to use the ENetMultiplayerPeer API, which provides a high-level network interface for handling network connections and sending and receiving data, as well as managing RPCs. By using ENetMultiplayerPeer, we can easily send and receive RPCs and handle network communication more straightforwardly.

When exchanging data with RPCs, objects can exchange data through functions, which makes the process more straightforward compared to exchanging data using UDP packets. With UDP packets, we need to send packets requesting procedures and wait for a...

Understanding the multiplayer authority

In Godot Engine’s high-level network API, the multiplayer authority is a concept that refers to the node that has the authority to make decisions about a node state in a multiplayer game. When two or more peers are connected in a multiplayer game, it is important to have a centralized peer that decides what changes are valid and should be synchronized across all connected clients.

The multiplayer authority is assigned to a specific peer in the game, usually the server or host, and this peer has the power to decide which changes from a given node should be accepted and synchronized across all connected clients. This is important because in a multiplayer game, multiple players may try to make changes to the game state at the same time, and it is the responsibility of the multiplayer authority to manage, verify, and synchronize these changes correctly.

Each connected client in a multiplayer game is assigned a unique peer ID, which is...

Comparing UDP and ENet approaches

The UDPServer and PacketPeerUDP classes are lower-level networking tools that allow for the exchange of data through UDP packets. This approach requires more work from us, as we must manage the sending and receiving of packets ourselves. For example, to create a login system using UDPServer and PacketPeerUDP, we would need to create a packet that contains the user’s login information, send it to the server, and then wait for a response.

In Chapter 2, Sending and Receiving Data project, we saw how to use UDPServer and PacketPeerUDP to pass data around. We saw that using these classes, we can serialize data and deserialize it on each end of the system, both client and server. Using this approach, we need to poll packets and wait for requests and responses to arrive. It does the trick, but you saw it can get a bit complicated.

One advantage of using the UDPServer and PacketPeerUDP classes is that they provide more control over the networking...

Remaking the login screen with RPCs

Welcome back to our studio, fellow network engineer! In Chapter 2, Sending and Receiving Data, we learned how to create a basic login system using the Godot Engine’s UDPServer and PacketPeerUDP classes. While this approach was perfect for our small-scale project, we need to level our game as we move forward and create a lobby!

Fear not, for we have the perfect solution for you – the Godot Engine’s ENetMultiplayerPeer and RPCs! These two powerful tools will help us build a robust and efficient system that can easily scale up to support multiple connected clients – as far as we researched, up to 4,095 simultaneously connected players!

With the Godot Engine’s ENetMultiplayerPeer, we can easily manage multiple connections and synchronize game data across all connected clients. This means that our login system will be able to handle more connections, and our game will run smoother than ever before!

With that...

Adding the player’s avatar

In any online game, the player’s avatar is a crucial element that represents them in the virtual world. In the previous section, we successfully authenticated the player and saved their session token and username in our AuthenticationCredentials autoload. Now, it’s time to use that information to display the player’s avatar in the lobby.

To achieve this, we will retrieve the player’s avatar information from our fake database and create a new AvatarCard, a custom scene with a TextureRect node to display the avatar’s image and a label to show its name. This way, players will be able to easily identify each other and feel more connected to the game world.

For that, let’s open the LobbyClient.gd script. Here, we are going to do three major things:

  1. Retrieve the avatar information from the server by making an RPC to the retrieve_avatar() method.
  2. Implement the add_avatar() method that LobbyServer...

Retrieving players’ avatars

In this section, we will implement the retrieve_avatar() method on the LobbyServer.gd script, which will allow players to request their avatar data from the server. The avatar data is stored in the fake database. The server will respond with some RPCs to update all players with the appropriate data, displaying their avatars in the shared lobby.

With this method in place, we will complete the functionality of our Lobby project. Players will be able to authenticate themselves and display their avatars in the lobby. This will provide a solid foundation for building more complex multiplayer games in the upcoming chapter, as the basics of networking have been covered.

Let’s do it!

  1. In the retrieve_avatar() method, check whether the user is logged in by verifying that the user exists in the logged_users dictionary. If the user is not logged in, exit the function:
    func retrieve_avatar(user, session_token):
        if not...

Testing the lobby

To test this out, we are going to run three instances of the game:

  1. Go to Debug | Run Multiple Instances and select Run 3 Instances.
Figure 3.3 – Choosing to run three instances in the Run Multiple Instances menu

Figure 3.3 – Choosing to run three instances in the Run Multiple Instances menu

  1. Then, open the res://03.making-lobby-to-gather-players/MainMenu.tscn scene and hit the Play button.
  2. Pick one of the instances to be the game’s server. To do that, just click on the Server button.
Figure 3.4 – Pressing the Server button on the MainMenu screen

Figure 3.4 – Pressing the Server button on the MainMenu screen

  1. Now, pick another instance and click on the Client button. It will take you to the LobbyLogin screen, where you can enter the first fake player’s credentials.
  2. Insert user1 in the username field and test in the password field. These are the credentials we added to FakeDatabase.json for our first user. Then, press the Login button. It will take you to the LobbyClient screen with a single...

Summary

In this chapter, we learned about RPCs and their importance in multiplayer game architectures. We saw how RPCs can be used to exchange data between nodes in the Godot Engine. We also saw what a multiplayer authority node is and how to set one up that manages all the game states between network peers. On top of that, we saw that by using the multiplayer API and ENetMultiplayerPeer, we can easily handle the communication between nodes.

Throughout the chapter, we created a lobby, which is a multiplayer game that features a lobby where players can join together. We saw how to create a client-server architecture, authenticate users, and exchange data between the server and the clients using RPCs. We also learned how to use the multiplayer API and ENetMultiplayerPeer to create a connection between the client and the server.

One of the essential concepts we learned is how ENetMultiplayerPeer simplifies the whole process of creating a multiplayer game compared to the low-level...

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}