Netcode Update - Inventory


πŸ€“ NetCode update

Thinking through how to keep game state in sync with network state, I had to choose a pattern to stay organized.

The problem:

  • No consistent state change interface
  • State changes were not clear if it triggers a network update, or comes from a network update
  • Signals were emitted from any object or Globals without reason
  • β€œUpdate/Updated” Signals cause co-recursive signal emission

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.

Before - not following a pattern

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 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

After - layered architecture

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)

network

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

offline

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

Get Camp Fire Craft

Leave a comment

Log in with itch.io to leave a comment.