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

Building an Online Checkers Game

In this chapter, we will delve into the captivating realm of creating an online multiplayer checkers game. We will apply the knowledge and skills we have acquired throughout this book to develop an engaging and interactive gaming experience.

Checkers, a classic board game enjoyed by players of all ages, provides the perfect canvas to explore the complexity of online multiplayer game development. We will learn how to leverage the power of the Godot Engine and its versatile features to create a seamless multiplayer experience that will have players strategizing, competing, and enjoying the game together.

To facilitate the synchronization of the game state across multiple players, we will introduce a powerful tool called the MultiplayerSynchronizer node. This node will play a crucial role in updating the positions of the checkers pieces across the boards of all connected players. By using this node, we can ensure that each player’s game view...

Technical requirements

In this chapter, we’ll be working with the fourth folder of our Godot Engine project repository, which you can access through the following link: https://github.com/PacktPublishing/The-Essential-Guide-to-Creating-Multiplayer-Games-with-Godot-4.0.

Another requirement you will need to accomplish before following with our project import is to download Godot Engine version 4.0, as this is the version we will use throughout the whole book.

After opening your Godot Engine 4.0, open the project using the project manager. Then, navigate to the 06.building-online-checkers folder. Here, you’ll find all the files we used to build this chapter’s project. You can test the game opening and playing the res://06.building-online-checkers/CheckersGame.tscn scene.

This scene showcases most of the features of our game. In this chapter, we are also going to implement the lobby system we have worked with throughout the book. On top of that, we will turn...

Introducing the Checkers project

Welcome, respected network engineer of our esteemed fictional indie development studio! As we embark on this chapter, let us take a moment to familiarize ourselves with the existing Checkers project. Currently, the project is designed for local multiplayers, allowing players to engage in thrilling matches offline. This understanding will serve as a solid foundation as we explore the path toward transforming our Checkers game into a captivating online multiplayer experience.

In this endeavor, our goal is to seamlessly transition the existing local multiplayer functionality into an online environment, without encountering significant obstacles along the way. By leveraging our existing knowledge and skills, we can effectively adapt the game to support online multiplayer, thus expanding its reach and providing players with the opportunity to compete with opponents from around the globe.

Throughout this section, we will unravel our Checkers project...

Serializing players’ turns

In Chapter 2, Sending and Receiving Data, we explored an essential technique to recreate the game state across multiple players in a network. By serializing the relevant data and transmitting it in small portions, we ensure efficient utilization of network bandwidth while maintaining synchronization among peers.

Developing an understanding of what information is crucial to replicate the game state among players involves mastering the concept of abstraction in game development. In our case, this primarily revolves around meta_board, which is an abstraction of the relevant metadata of our game, such as the positional data and king state of the Pieces and the empty cells in the board.

Additionally, we need to consider the availability of Pieces, depending on the players’ turn. Fortunately, most other elements of the game can be managed locally without requiring network synchronization.

To simplify the process of synchronizing node properties...

Handling remote turn shifts

One of the most important aspects of playing a game online is to maintain players’ autonomy and authority over their resources – in this case, their team’s Pieces. Godot Engine offers an interesting system where a SceneTree can structure its nodes’ hierarchies with distinct Multiplayer Authorities.

To set up a node and its children’s Multiplayer Authority, recursively, we can use set_multiplayer_authority() and pass the respective peer’s ID as an argument. In our case, we are going to change the BlackTeam and WhiteTeam nodes’ Multiplayer Authority to match their respective players’ peer IDs.

This will be done by the server, so to keep the application simple, we are going to allow clients and server to share the same script, and we will check which one is running the server instance by using is_multiplayer_authority() on the CheckerBoard. We should only run this logic if the game is running in...

Managing win and lose conditions

Excellent! We have successfully completed the development of the CheckerBoard scene, and our game’s core functionalities are now in place. The next step is to transition the logic of the CheckersGame scene from local to remote gameplay.

To begin, let’s open the res://06.building-online-checkers/CheckersGame.tscn file and familiarize ourselves with its structure.

Figure 6.9 – The CheckersGame’s scene node hierarchy

Figure 6.9 – The CheckersGame’s scene node hierarchy

Take note that the CheckerBoard’s player_won signal is connected to the CheckersGame._on_checker_board_player_won() callback. This callback is responsible for handling situations when a player’s team has no remaining pieces on the board. Now, let’s proceed by opening the script for CheckersGame.

We will be working on all the methods within the script, ensuring they are properly adjusted for online multiplayer functionality:

  1. First of all, let’...

Summary

To recap, in this chapter, we introduced the MultiplayerSynchronizer node to synchronize properties across a network, established the concept of abstraction for effective data transmission, utilized the @rpc annotations to enable multiplayer functionality, and learned how to assign and manage Multiplayer Authority to ensure player autonomy and resource protection.

In the upcoming chapter, we will see how to develop an online Pong game. There, we will cover the modifications necessary to turn the local game into an online multiplayer one, setting up online multiplayer paddles, syncing remote objects in real time, and coordinating the paddle’s position. For that, will use the MultiplayerSynchronizer node with a bit more depth than we did in this chapter. Also, we will talk about the importance of maintaining a shared game world for players in action-based games, which is very different from turn-based games.

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