Practical Godot Networking: MPEVMVP technical discussion

Godot’s High Level Networking tutorial (and book chapter) do a great job of explaining a peer-to-peer networking setup. However, many games want to be server-authoritative, and to set up a multi-area game, you sort of need to. Luckily, you can use the same primitives to build a server/client game. This is just sort of a brain dump of my notes that may be helpful in navigating the MPEVMVP source. Feel free to post any questions you have about it; I love to talk about this stuff.

The technical requirements that deviate from the basic tutorial they hand out are as follows:

  • Server-authoritative client/server game
  • Multiple levels that players can switch between
  • Players are able to join a match in progress

In case you haven’t tried it, the project is here: https://github.com/eamonnmr/mpevmvp. The gameplay is similar to Asteroids (and, well, similar to Flythrough.Space): it uses RigidBodies to simulate movement, you can shoot other players to force them to respawn, and you can press “J” to initiate a hyperjump after you’ve pressed “m” to select a star system from the map.

Godot’s Networking Golden Rule

A remote call (rpc, rset) is going to be called on the remote node with the same node path. That includes name of the node, the name of the parent, etc, all the way to the root node. You can set a child node’s name with set_name but if a similarly named child already exists, an @number will be added to your node. This is a major source of bugs for networked projects, so be careful. You cannot set a node to a name with @ in it. So in effect, you cannot use Godot’s auto-incremented names (since they append @increment) to sync between the client and the server. For this reason, I used a uuid library for generating node names.

The need for the same structure on client and server despite the fact that the server held a whole universe while the client only cared about one system is a major source of complexity in the codebase.

Server Authoritative

I implemented the Client and Server as singletons that exist on both sides. The reason for this is that it makes it really readable; when the client needs to send something to the server you write Server.rpc and when the server needs to send something to the client, you use Client.rpc. Besides this line of communication, I’ve used rpc and rset within various nodes to update and alter the client versions of themselves. This always happens inside an is_network_master block, to ensure that it’s being called on the server version of the game.

The one thing we do need to gather from clients is input, and for that each client has a PlayerInput node, network_master’d to them. They rset_id input up to the server. The server copy of the player nodes looks at this remote copy of the player_input to determine it’s behavior, then pushes its state back down to all clients.

Multi Level

Suppose you want to have multiple different levels/worlds/rooms in your game. The trick is that you need to mesh the following two constraints:

  • Node paths need to be the same between client and server (see discussion above)
  • The server needs to have different node paths for each level

The trick, then, is to send the client to the new level by loading the appropriate level and making sure it’s named appropriately. There’s one wrinkle though: different 2d physics worlds need to use a Viewport class, but you don’t want to add additional viewports on the client for performance reasons (tried it, it was way too slow.) My solution was to add an extra layer of nodes called “universes” in the comments which are a Node2d on the client but a Viewport in the server. They are managed on the server by ServerMultiverse and on the client by ClientUniverse. Client Universe cares about a single “universe” child whereas ServerMultiverse handles multiple children.

Sending Entities/Switching Levels

In order to join a match in progress or switch to a level that already has stuff in it (or, indeed, switch a player into a level) we need a robust way for the server to move fully-formed nodes to the client. Godot does not provide a generic “send this node” method, so we need to write one for ourselves. The approach I took was:

  • Assume that the ent will be instanced from the same scene that the ent on the server is
  • Write a “serialize” and “deserialize” method that allows it to dump a dictionary with all of its important state and recreate itself from that dictionary.
  • Make sure the node on the client side is assigned the same name as the one on the server side.

So when a client’s level is switched, they dump the current level and recreate the new one by loading in the scene and then, node by node, deserializing the state sent from the server. As it turns out, this means that any state can be synced freely, and it enables us to dispense with the lobby entirely and let players join and leave at will.

Sync In Multiple Levels

Each level owns the process of syncing its state to clients in that level. Scenes within the level can declare a method that serializes their state for a net frame, and the level gathers all of those up and sends them. The client receives the frames from the server, and the individual scenes use the content of those frames to interpolate their position. To save bandwidth, I only do this for ships and guided projectiles, everything else moves deterministically. This whole process is derived from the teaching of (and better described by) this video.

Player Lifecycle

Because we’re using the player’s presence in a level to determine if we should update entities in that level for that player, when a player’s avatar is destroyed the world seems to stop for that player until they respawn. This might be desirable in some games, but I think most games at least want to show the player the immediate seconds after their own demise. So what we do is replace the player with a ghost.

Tooling & Workflow

GDScript is based on Python 3 and the most basic syntax resembles it. ‘func’ in place of ‘def’, no ‘self’ and no list or dict comprehensions or other advanced features. It feels like the python of basic python tutorials. However in exchange for this, you get one thing python 3 does not offer: true static typing. In Python 3 you can offer a type hint for a function argument but it is not truly enforced. GDscript enforces the types at compile time and boy does it ever catch a lot of bugs. You also get the ability to define classes with typed members, similar to what you get in Python with Pydantic. Overall I like gdscript and the things I miss aren’t dealbreakers.

The lack of a vi mode for the editor hurts. I know I could just use the godot plugin for vi, but I actually really like staying inside Godot’s editor. Despite the slightly crowded layout, it’s really very nice. The autocomplete to node paths is a killer feature that makes dealing with complicated trees less of a pain.

Conclusion

Building a multiplayer game in Godot is possible but by no means easy. The high level multiplayer API despite its gotchas works well, but you need to implement the sync logic yourself. This shouldn’t be too surprising as games have very different needs when it comes to net code. I hear that a similar mechanism for frame based sync and lerping is going to be more built-in in godot 4, which would be sweet. The puppet system (which I initially used; you can see a version in the tutorial) seems to be useful only for very low latency situations, so I regret that it’s a first-class feature because it may lead people down a rabbit hole of net code that will not work for most games.

MP EV MVP Retro

What I’ve learned from breaking my own rule.

I remade a game again

EVMVMVP is the awkward initialism for my latest project; a multiplayer EV-like. You can download it here, and look at the source code here. I know I said I shouldn’t do it, but I really wanted to learn Godot and having a multiplayer server with multiple levels seemed like a really interesting problem to tackle. Implementing a bunch of stuff I already know pretty well but in a new engine and language has been a fun ride. I’ve jotted some notes down about the project so far. I’ve saved a deep technical discussion of Godot for another post.

Using an engine is well worth it

After flythrough.space I decided that I wasn’t going to build too much from scratch next time, not was I going to fight against tooling. The options I considered for FTS where Panda3D and BabylonJS. However over the course of that project also tried out Unity and Godot for small side demos and ended up liking Godot (and it’s permissive license and thriving community) enough to go all-in. In retrospect this was the absolute right move and I regret nothing.

Working With Onyx’s Sprites

I’ve been staring at these sprites on and off for something like fifteen years, itching to put them into a game. The original models used to render them are long gone, but Onyx (in a tutorial) explained how to separate out the engine glows. Working with these sprites feels like stepping out of time. They’re from the 90s. There are dithering tricks here I wouldn’t even imagine. They use a tiny palette but they’re so damn weighty. Present. Metallic when they need to be, plastic when they want to be. They tend towards the Star Wars / Used Future look. They invite stories to be written about them. And the planets are just absolutely lush too. Something about that limited palette…

Testing with real people is essential

I’ve been nagging more people to play this one. On the one hand, it’s much easier to convince someone to play your game if you can play it with them. On the other hand, you really need to do that testing or you will (like I did) find out that the sync method you’re using is totally busted and you need to replace it with interpolation. Furthermore, you’ve got no idea if your game is fun to anyone but yourself unless you subject it to ruthless testing by people who will give honest feedback. It becomes very easy to get settled in and work around horrible UI issues you’ve coded because “well, I know how it works.”

The ability to test solo is essential

Wrangling people for a test is hard. You need to be able to fire up an instance of your server and client with minimal effort or you’re going to wait too long and leave a bunch of bugs that you’ll be scrambling to fix when you finally get people around to test with. And absolutely nothing is more frustrating than the stars aligning for a test only to have a showstopper rear its head at the worst possible moment.

Procedural Generation

I had a conversation with a coworker about EV games and roguelikes. They made a claim that I’m not sure is true but definitely opened my eyes-they thought that the star maps in EV games probably had been procedurally generated-once.

Actually hand-crafting a full EV map is a bit of a process. One thing I never finished doing in FTS was populating all of the systems.

Meanwhile, Mag Steel glass has developed a really cool universe generator spreadsheet. I figured I could leverage it to make a map. Traditional EV games require you to specify exactly who spawns in what star system and FTS carried on that painful tradition. For this one I had simpler rules; factions had starting locations and then grew “influence” where planets (or stations) would belong to them and their ships would spawn. If two areas of influence overlapped, they’d fight.

Document enough for people to get up and running locally

Port forwarding is a lost art. There was much grumbling when my friends and I went to go start a game of Valheim; luckily I’d learned to properly port forward as part of this project! If you need a server for people to play your game, you can’t rely on your master server; you’ve got to give people an option to join localhost and put it in your manual/readme. Having images in the readme also helps show people what your project is about; this is especially important for games.

You don’t get cross platform for free

Using cross platform technology does not absolve you from cross platform testing. All of your target platforms might have the same features but they may all have different bugs for you to work around. I’m developing on linux out of what is probably just bloody mindedness at this point, so when I did my live demos on windows I often learned exciting new things like ‘your sound exports are busted’ or ‘only one computer can be assigned to a given port by a given router’ or ‘you need to version bump the editor for correct mac exports.’ Guess which one of those lead to an incredibly embarrassing platform bug report!

Video Tutorials are worth it

Part of my commitment to overcoming my stubborn hangups was relenting and watching video tutorials. I still strongly prefer written tutorials as a medium but the content is moving to video. For example:

This (and the followup on extrapolation) gave me enough to totally re implement my networked movement.

Working In Public

I post progress updates on discord channels full of fans from the old days, and regularly demo the game with friends. This has been a source of motivation and helped me stick to a more iteration focused (and less “well, I’ll go off for a month and make all of the models/textures/features”) work habit. I’m not sure I’m going to do this for every project; part of why I did it for this one was I wanted to spur participation and that didn’t really happen. Very few people who I didn’t know personally went to the trouble of getting the game running. Part of that is because it wasn’t immediately approachable (you need to host or find a server then run another client to join it) and wasn’t immediately grippy (ok, I can buy a ship, now what?) Any amount of friction is going to turn away potential players, and if you’re used to playing rough unfinished demos this can become a blind spot. But beyond even that, getting someone to try something new is just plain hard.

People respond to Video

At the end of the day, pictures and text don’t cut it for sharing a game. Games are animation. People are used to watching streamers. You need a video of your thing and it needs to show off what the thing is about, including how you interact with it and why it’s compelling. This was my attempt:

Have features and fixes gone in since that video was made? Sure. But it’s still the best tool to show off what the project is about.

Parting Shots

Has the project met its goals so far?

  • Learn the Godot engine: ✔️

I’m comfortable with the technology now; expect future prototypes to be more rapid.

  • Use Onyx’s Sprites in a game: ✔️

I used most of the ship sprites themselves and lovingly separated out the engine glows for even the ones I didn’t use, in case someone in the future wants to use them again. Cosmic Frontier mod anyone?

  • Build a reusable platform for multiplayer EV-clones: ✔️

Maybe this is a partial check. If someone has an idea and that idea is based on multiplayer EV gameplay, I’m confident that if they’re willing to learn Godot they could at the very least copypasta big chunks of the MPEVMVP codebase and get something running. Or they could fork it, replace everything, add missions, remove multiplayer, and make an EV clone real fast.

  • Discover if EV gameplay works in multiplayer: ✔️

Learned important lessons about how much bullet hell spam a networked game can handle, and as a straight dogfighting sim it let me figure out what is and isn’t fun in PVP EV.

  • Build a game people want/like/play: ❌

As far as I can tell, nobody enjoyed playing this besides on demo night. The service I think people want this game to be is beyond the scope of what it can be with a team this size.

4/5 ain’t bad. Thanks a bunch to everyone who tested it, gave feedback, and flew too far to find the center of the map again.