Real-time Networked Godot Objects with Nakama Server
For this project I wanted to prototype a 2D multiplayer twin-stick shooter game similar to Enter the Gungeon. I chose to write the game client using Godot 4.2 and to use Nakama for the game server.
In the video below you can see 4 separate game clients running against the same Nakama server. Clicking on a window brings it into focus and activates the input listener for that client - this prevents keystrokes and mouse clicks from being picked up by the other clients since they’re all running on the same machine.
When a game client is in focus the mouse cursor is replaced by a green circle indicating where the player will fire their weapon. The player sprite will also rotate so its always “looking at” the reticle. This rotation is tracked on the Nakama server as well so all other players will be able to see the orientation of all other players in the same match.
Clicking anywhere on the map will cause the player sprite to shoot a green projectile towards the reticle. Players can also move around the map with the WASD keys. The players and green projectiles are both networked game objects so they are tracked by the Nakama server and can interact with each other.
The Nakama server receives predefined network opcodes from each game client that is then relayed to every other authenticated game client participating in the match. For example, here is the function that a game client calls to tell the Nakama server about its latest position data:
From here, the Nakama server receives this message, authenticates the sender, then relays this message to the other 3 game clients who handle the network message with the following function handler, which is simply a GDScript match statement that switches on each opcode:
Each opcode is assigned an integer value constant that all other game clients share: