Skip to main content

Multiplayer Cooking & Assembly System Implementation

This document outlines the architecture and implementation of our AAA-standard multiplayer cooking and assembly system.

Built for extreme network performance, this system utilizes a Data-Driven Architecture. Instead of parenting networked GameObjects to one another (which causes massive bandwidth bloat and lag), the system syncs a lightweight, fixed-size data struct (NetworkItemData.cs) and dynamically updates purely visual meshes on the client side.


Core Architecture Overview

The system is split into three distinct layers:

  1. The Data Layer: Dictates what an item is, using integer IDs instead of strings.
  2. The Visual Layer: Purely cosmetic 3D models with NO physics or network logic. Used when items are held by players or sitting on counters.
  3. The Physical Layer: Heavy GameObjects with Rigidbodies, Colliders, and NetworkTransforms. Used only when items are dropped on the floor.

Core Scripts & Files

ScriptResponsibility
NetworkItemData (Struct)A fixed-size struct (zero memory allocation) containing a Base ID, 5 ingredient slots, and an IsOpen state. Sent over the network instead of GameObjects.
ItemSO (ScriptableObject)The permanent data template for an item (Name, Icon, Category, Visual Prefab, Physical Prefab).
ItemDatabaseSOAssigns a permanent ushort Network ID to every ItemSO for ultra-cheap network syncing.
InventoryManagerHolds a SyncArray<NetworkItemData> representing the player's pockets/hands.
Item.csAttached to physical floor prefabs. Holds the NetworkItemData struct so burgers don't lose their ingredients when dropped.
DynamicMealVisuals.csSits on complex visual prefabs (like a Bun or a Box). Reads the NetworkItemData struct and turns child meshes ON/OFF depending on the ingredient IDs inside the struct.
ClearCounter.csThe assembly station. Allows players to combine single ingredients from their hand into the NetworkItemData struct resting on the counter.

🍔 Tutorial: Building a 10-Item Burger Box

This guide walks you through creating a complex, interactable container (a Burger Box) that requires a base item (Bottom Bun) and can hold up to 10 distinct ingredients.

Step 1: Create the Data (ItemSO)

  1. In Unity, right-click and create your ItemSO assets:
    • BurgerBox (Set Category to Container)
    • BottomBun, CookedMeat, Cheese, Tomato, Salad, Onion, Pickles, Bacon, Sauce, TopBun.
  2. Open your ItemDatabaseSO and add all these items to the list so they receive a unique ushort Network ID.

Step 2: The Visual Prefab (BurgerBox_Visual)

The Visual Prefab is responsible for making the item look good when sitting on a counter. It contains all possible ingredients nested inside it, hidden by default.

  1. Create an empty GameObject named BurgerBox_Visual.
  2. Add the Box Bottom Mesh and the Box Lid Mesh as children.
  3. The Nesting Trick: Add a new empty child object called Burger_Visuals_Root.
  4. Drag the 3D meshes for all 10 ingredients (Bottom Bun, Meat, Cheese, etc.) inside Burger_Visuals_Root. Stack them perfectly so they look like a finished burger.
  5. Deactivate (Turn OFF) all the food meshes.
  6. Add Components to the Root (BurgerBox_Visual):
    • Animator: Assign an Animator Controller with an IsOpen boolean parameter.
    • ContainerVisuals: Drag the Animator into this script.
    • DynamicMealVisuals: * In Required Base Item, assign the BottomBun ItemSO. (This prevents players from putting a loose tomato directly into the box).
      • In the Ingredient Mappings array, create 10 elements. Map each ItemSO to its corresponding child mesh in the hierarchy.

Step 3: The Physical Prefab (BurgerBox_Physical)

This prefab is spawned when the player presses the drop button. It requires physics to fall to the floor.

  1. Duplicate the BurgerBox_Visual prefab and rename it to BurgerBox_Physical.
  2. Keep the entire visual setup (the meshes, the DynamicMealVisuals script, the Animator).
  3. Add Physics & Network Components:
    • Rigidbody: Enable 'Use Gravity'.
    • BoxCollider: Scale it to encapsulate the closed box.
    • Network Transform: To sync its physical position when kicked or dropped.
    • Item.cs: * Drag the BurgerBox ItemSO into the Starting Item Data slot.
      • Drag the Rigidbody into the Item Rigidbody slot.
      • Drag the attached DynamicMealVisuals component into the Meal Visuals slot.
  1. Go back to your BurgerBox ItemSO in the Project folder.
  2. Drag BurgerBox_Visual into the Visual Prefab slot.
  3. Drag BurgerBox_Physical into the Physical Drop Prefab slot.

Step 5: Test the Gameplay Loop

  1. Spawn the Box: Place a Box Dispenser in your scene. Interact to get the Box.
  2. Place the Box: Interact with an empty ClearCounter. The BurgerBox_Visual spawns. Because it's a Container, the counter automatically sets IsOpen = true, playing the open animation.
  3. Assemble: Dispense your ingredients. You must place the BottomBun first (due to the Required Base Item rule). Then, stack the Meat, Cheese, and Tomatoes. The DynamicMealVisuals will turn on the meshes inside the box.
  4. Close the Box: Look at the box with empty hands and press InteractAlternate (F). The counter updates IsOpen = false, and the lid animation plays.
  5. Drop it: Pick up the closed box and press Drop (Q). The BurgerBox_Physical drops to the floor. Because Item.cs holds the struct, it remembers the box is closed, and it remembers exactly which 10 ingredients are inside it!