Inventory Replica
Overview
The Inventory replica component serves as a network-transparent layer that manages inventory interactions for players (and other entities with inventory support) in Don't Starve Together. It coordinates between the local inventory component (on the server) and a remote inventory_classified prototype (the classified proxy), enabling clients to display and manipulate inventory state without direct access to the authoritative component. It handles opening/closing the inventory UI, equipping items, managing overflow containers, and synchronizing specialized states such as heavy lifting and floating (e.g., for Werebeaver/Woodie transitions).
Dependencies & Tags
- Component dependency: Requires
inst.components.inventory(server-only) and/orinst.inventory_classified(networked classified proxy). - Tag dependency (server side): Uses
playerto determine initialization path;corpseto conditionally hide inventory when revived. - Tags used: None added or removed by this component itself.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | None (injected) | Reference to the entity the inventory belongs to. |
opentask | Tasks or nil | nil | Delayed task used to open the inventory UI; cancellable to prevent race conditions. |
classified | Component (inventory_classified proxy) or nil | nil | Reference to the networked classified proxy object used to replicate inventory state to clients. |
ondetachclassified | function or nil | nil | Callback function registered to handle entity removal of the classified object. |
Main Functions
Inventory:OnOpen()
- Description: Triggers server-side to open the inventory UI for the entity. Sets the classified proxy’s visibility to true and designates this entity as the target.
- Parameters: None.
Inventory:OnClose()
- Description: Closes the inventory UI by hiding it. Cancels any pending open task and sets classified visibility to false.
- Parameters: None.
Inventory:OnShow()
- Description: Makes the inventory UI visible without opening (e.g., for hotkey toggles or state transitions).
- Parameters: None.
Inventory:OnHide()
- Description: Hides the inventory UI if already open/visible.
- Parameters: None.
Inventory:SetHeavyLifting(heavylifting)
- Description: Updates the
heavyliftingstate on the classified proxy and refreshes relevant action filters (e.g., forWerebeaver carry behavior). - Parameters:
heavylifting(boolean) — Whether the entity is currently engaged in heavy lifting.
Inventory:SetFloaterHeld(floaterheld)
- Description: Updates the
floaterheldstate (e.g., for Woodie’s floating behavior) and adjusts state machine events + action filters accordingly. - Parameters:
floaterheld(boolean) — Whether the entity is holding a floater (e.g., boat/raft).
Inventory:AttachClassified(classified)
- Description: Associates a classified proxy object (typically received from the server) with this replica. Registers event listeners and initializes UI重建.
- Parameters:
classified(Component) — Theinventory_classifiedproxy to attach.
Inventory:DetachClassified()
- Description: Detaches the classified proxy, clears listeners, and resets UI visibility and active item state on client.
- Parameters: None.
Inventory:GetNumSlots()
- Description: Returns the number of inventory slots available. Falls back to default max slots if
inventorycomponent is absent. - Parameters: None.
Inventory:GetItemInSlot(slot)
- Description: Returns the item stored in the specified inventory slot. Delegates to
inventoryorclassifieddepending on context. - Parameters:
slot(number) — Zero-based slot index.
Inventory:EquipActiveItem(), Inventory:EquipActionItem(item), Inventory:SwapEquipWithActiveItem(), etc.
- Description: A family of functions for common inventory interactions: equipping active item, equipping a specified item, swapping with equipment slot, etc. All delegate to either the local
inventorycomponent or theclassifiedproxy, ensuring same behavior across authoritative and replicated contexts. - Parameters: Vary per function (e.g.,
item,slot,eslot,container).
Inventory:Has(prefab, amount, checkallcontainers), Inventory:HasItemWithTag(tag, amount)
- Description: Queries item existence with optional count and container-scanning. Falls back to
classifiedimplementation wheninventorycomponent is unavailable. - Parameters:
prefab/tag— Query target.amount(number) — Minimum required count.checkallcontainers(boolean, optional) — Whether to include nested containers.
Inventory:GetOpenContainers(), Inventory:GetOverflowContainer()
- Description: Returns tables of currently open containers and the overflow container (e.g., backpack), respectively. Handles both authoritative (
inventory) and replicated (HUD.controls.containers,classified) sources. - Parameters: None.
Inventory:IsHeavyLifting(), Inventory:IsFloaterHeld(), Inventory:IsVisible(), Inventory:IsOpenedBy(guy)
- Description: Read-only queries for current inventory state. Each uses local
inventorycomponent if present, otherwise falls back toclassifiedproxy values. - Parameters: Vary per function (e.g.,
guyforIsOpenedBy).
Inventory:Open() (via self.opentask hook)
- Description: Internal helper used in delayed task (
DoStaticTaskInTime) to open the inventory and optionally hide it if the entity is a corpse. - Parameters: None (defined as
local function OpenInventory).
Events & Listeners
-
Listens to
inst:ListenForEvent("newactiveitem", ...)— Syncs active item from inventory to classified proxy (server → client). -
Listens to
inst:ListenForEvent("itemget", ...)— Syncs slot content changes (server → client). -
Listens to
inst:ListenForEvent("itemlose", ...)— Handles item removal from slots (server → client). -
Listens to
inst:ListenForEvent("equip", ...)— Syncs equipment slot updates (server → client). -
Listens to
inst:ListenForEvent("unequip", ...)— Syncs unequip events (server → client). -
Listens to
classifiedevents:visibledirty→ Toggles crafting/inventory UI visibility.heavyliftingdirty→ Updates action filters (heavy lifting) and context.floaterhelddirty→ Updates action filters and state machine events (floating).
-
Listens to
"onremove"onclassified— Triggers detachment logic when classified proxy is destroyed. -
Triggers
inst:PushEvent("newactiveitem", {})— Clears active item reference when UI is closed. -
Triggers
inst:PushEvent("inventoryclosed")— Notifies systems that the inventory is closed.