Skip to content

Multiplayer Scripting

Valk edited this page Nov 10, 2022 · 28 revisions

This page is a work in progress, if you have any questions please ask valk#9904 in the Sankari discord.

An introduction

The last time I looked at the multiplayer code was a while ago for sure, so that is why I am writing this. To document everything so others can follow along.

Set the linker settings to the following for debugging multiplayer.

image

image

The Auto Host Join option makes it so if the game is launched through the editor a server will be setup on local port and host client will auto connect to this server. If the game is launched through an exported release, a client will auto attempt to join this local host server. The game window titles are updated to make this more clear. If you do not check Auto Host Join, you will have to go to the map screen and press Esc to open up the "UI Map Menu" which gives you all the buttons needed to host or join a server.

The UIMapMenu.cs script is located in Scripts/UI/UIMapMenu.cs, here you can find functions like HostGame(...) and Join(...). This is where all the multiplayer code starts. Note that you will see use of the Tokens class here, this is used to create cancellation tokens to cancel networking tasks later down the line. I recommend you look through the code to see how these are used.

All the rest of the multiplayer code (netcode) is located in Scripts/Netcode. The main areas of interest are Scripts/Netcode/Client, Scripts/Netcode/Server and Scripts/Netcode/Packets. Note that you will see for e.g. ENetServer and GameServer. ENetServer is the underlying enet implementation and as such all game specific code should go into GameServer. Same deal with ENetClient and GameClient.

Packets

Lets talk about packets. Packets are how data is sent over the network. Each packet has its own opcode defined in Netcode/Packets/_Opcodes. Some packets have a secondary or even a third opcode to help prevent going over the 255 byte limit. (The first packet opcodes are sent as a byte to save bandwidth, this means we can't have over 255 opcodes). As such organizing the packet opcodes is critical. Stuff like lobby creation / deletion opcodes should all get one "Lobby" opcode and use secondary opcodes. There are no lobby packets but this was just used as an example. Back to packets. Have a look at the CPacketXXX and SPacketXXX scripts for examples on how to read / write packets from / to client / server.

There are many methods you can use to send packets from the server to clients. These methods can be found in ENetServer.

image

There are only 2 methods for clients to send packets to the server.

image

Thread Safety

This game makes use of 3 threads (Godot, Server, Client). Do not directly access public variables or methods from these threads to other threads. If you want to communicate between threads please make use of the appropriate ConcurrentQueue<T> channels. Violating thread safety can lead to frequent random game crashes with usually no errors in console making these types of issues extremely hard to track down when they start acting up.

This issue will stay up until either the wiki is updated with this information or the multiplayer code becomes insanely easy to use it does not even need a guide

Ignoring Opcode Log Messages

In the future a checkbox option will be added to completely turn opcode logging off.

image

Clone this wiki locally