Posted February 19, 2022 by GomaGames
#netcode #coding
Thinking through how to keep game state in sync with network state, I had to choose a pattern to stay organized.
The problem:
Take the Inventory as an example, player can add, remove, or update an inventory, and the items in the inventory need to be synchronized to all other connected players.
βββββββββββββββββ βββββββββββββββββββββββββββββ
β Inventory.gd β β Network.gd β
β β β β
β add() ββββββββ€βΊsend_add() β
β remove() ββΌβΌβΌβΌβΌβΌβ€βΊsend_remove() β
β update() ββββββΌβββββββ€βΊsend_update() ββββββ β
β β² β β βΌ β
ββββ¬βββββββββββββ ββββΌβsignal(inventory_updated) β
β β β β
ββββββββββββββββββ βββββββββββββββββββββββββββββ
The problem is clear when updating an inventory, and listening for an inventory update
Iβve added a βNet Syncβ module as a broker layer between the actual βNetworkβ comms layer.
Each layer should only interact with immediately adjacent layers.
If the player is online (in a real game)
If the player is offline (local dev/debug and game design)
The βNet Syncβ layer skips talking to network socket and echoes the same data from the send()
signals
The public interface for Inventory
is βsafeβ to use and the code can trust that recv()
signals will be emitted in both online and offline modes