Inventory
Overview
This component handles item management for an entity, including storing items in slots, equipping items, tracking the active item, and supporting stacking, overflow containers (like backpacks), and inventory operations such as giving, dropping, removing, and transferring items. It is primarily used for player characters and other entities capable of holding items, and it integrates closely with the Entity Component System (ECS), persistence (save/load), UI interactions, and event-driven behavior.
Dependencies & Tags
- Components:
- Relies on
inventoryitem(on items),equippable(on equipment),armor,stackable,container,petleash,migrationpetowner,resistance,damagetyperesist,setbonus,waterproofer,playerfloater,inspectable,spellbook.
- Relies on
- Events listened to:
"death"→ triggersOnDeath, which drops all items."player_despawn"→ triggersOnOwnerDespawned, which sends"player_despawn"to active items and equipped items.
- Tags:
- It does not directly add or remove entity tags.
- Reads entity tags (
"heavy","backpack","nocrafting","pocketdimension_container") on items and the owner.
- Special behaviors:
- Registers owner listen-for-event hooks for death/despawn.
- Uses
replica.inventory(network replica) to sync state likeSetHeavyLifting,SetFloaterHeld,OnOpen,OnClose, etc. - Uses
SourceModifierList(boolean)forisexternallyinsulated.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | nil | The entity the inventory belongs to. Set in constructor. |
isopen | boolean | false | Whether the inventory UI is currently open. |
isvisible | boolean | false | Whether the inventory UI is visible. |
ignoreoverflow | boolean | false | Flag to bypass overflow container behavior (used during transfers). |
ignorefull | boolean | false | Flag to bypass "inventory full" logic (used during container moves). |
silentfull | boolean | false | Suppresses "inventoryfull" events. |
ignoresound | boolean | false | Suppresses "gotnewitem" events. |
itemslots | table | {} | Map of slot indices (1-based integer) to non-equipped items. |
maxslots | number | GetMaxItemSlots(TheNet:GetServerGameMode()) | Max number of inventory slots. Becomes read-only in classified mode. |
equipslots | table | {} | Map of equipment slot names (EQUIPSLOTS.*) to equipped items. |
heavylifting | boolean | false | Set to true when a "heavy" BODY slot item (e.g., Beefalo Hat) is equipped. |
floaterheld | boolean or nil | nil | Set to true when a floater item is held in HANDS. |
activeitem | Entity or nil | nil | The item currently in hand. |
acceptsstacks | boolean | true | Whether this inventory can accept stackable items (read-only in classified mode). |
ignorescangoincontainer | boolean | false | Bypasses inventoryitem.cangoincontainer checks. Read-only in classified mode. |
opencontainers | table | {} | Set of containers currently open by the owner. |
opencontainerproxies | table | {} | Set of container proxy instances. |
dropondeath | boolean | true | Whether the inventory drops all items on death. |
isexternallyinsulated | SourceModifierList(boolean) | SourceModifierList(inst, false, SourceModifierList.boolean) | Tracks if the entity is externally insulated (e.g., via external sources). |
Main Functions
Inventory:NumItems()
- Description: Returns the number of distinct items stored in
itemslots. - Parameters: None.
Inventory:NumStackedItems()
- Description: Returns the total number of items accounting for stack sizes in
itemslots(active item and equipped items are excluded). - Parameters: None.
Inventory:TransferInventory(receiver)
- Description: Moves all persistent items and equipped items from this inventory to
receiver's inventory. Handles equippable restrictions and persistence flags. - Parameters:
receiver(Entity withinventorycomponent).
Inventory:SwapEquipment(other, equipslot_to_swap)
- Description: Swaps equipped items between
selfandotheron specified or all equipment slots. Respects equippable restrictions and attempts to equip if possible; otherwise, gives as item. - Parameters:
other(Entity),equipslot_to_swap(optional slot name). Returnstrueifotherhas a valid inventory.
Inventory:OnSave()
- Description: Returns save data and item references for persistence. Includes items, equipment, and the active item (if not equipped). Skips non-persistent items.
- Parameters: None.
- Returns:
data(table),references(array of entity references).
Inventory:OnLoad(data, newents)
- Description: Loads inventory contents from saved data, including migration of pets and containers. Flags
isloadingtemporarily. - Parameters:
data(table withitems,equip,activeitem),newents(table of new entities for spawn).
Inventory:DropActiveItem()
- Description: Drops the active item, sets active item to
nil. - Parameters: None.
- Returns: Dropped item (or
nil).
Inventory:ReturnActiveActionItem(item, instant)
- Description: Returns an item back to inventory or active item if it matches
self.activeitemand buffered action conditions match; bypasses fullness checks using internal flags. - Parameters:
item(Entity),instant(boolean).
Inventory:HasAnyEquipment()
- Description: Returns
trueif any equipment is equipped. - Parameters: None.
Inventory:IsWearingArmor()
- Description: Returns
trueif any item has thearmorcomponent equipped. - Parameters: None.
Inventory:ArmorHasTag(tag)
- Description: Returns
trueif any armor-equipped item hastag. - Parameters:
tag(string).
Inventory:EquipHasTag(tag)
- Description: Returns
trueif any equipped item hastag. - Parameters:
tag(string).
Inventory:EquipHasSpDefenseForType(sptype)
- Description: Returns
trueif any equipped item provides special defense forsptype. - Parameters:
sptype(string, special damage type).
Inventory:IsHeavyLifting()
- Description: Returns
trueif currently heavy-lifting (i.e., "heavy" BODY slot item equipped). - Parameters: None.
Inventory:IsFloaterHeld()
- Description: Returns whether a floater is currently held in HANDS slot.
- Parameters: None.
Inventory:ApplyDamage(damage, attacker, weapon, spdamage)
- Description: Applies damage to the entity, absorbing through armor and resistances. Handles special damage (e.g., fire, freeze) and durability loss.
- Parameters:
damage(number): Base damage amount.attacker(Entity ornil).weapon(Entity ornil).spdamage(table of special damage types to amounts, optional).
- Returns:
leftover_damage(number),spdamage(table ornil).
Inventory:GetActiveItem()
- Description: Returns the current active item.
- Parameters: None.
Inventory:IsItemEquipped(item)
- Description: Returns the equipment slot name if
itemis equipped; otherwisenil. - Parameters:
item(Entity).
Inventory:SelectActiveItemFromEquipSlot(slot)
- Description: Unequips an item from
slotand makes it active. If no space, gives it back to inventory. - Parameters:
slot(string, equipment slot name). - Returns: New active item.
Inventory:CombineActiveStackWithSlot(slot, stack_mod)
- Description: Attempts to merge the active item stack into an item at
slot(same prefab/skin, stackable). Increases/decreases stack sizes. - Parameters:
slot(number/string),stack_mod(boolean, optional).
Inventory:SelectActiveItemFromSlot(slot)
- Description: Moves item from
slotto active item, removing it from inventory slot. - Parameters:
slot(number, inventory slot index).
Inventory:ReturnActiveItem(slot, stack_mod)
- Description: Returns active item to specified slot or drops it if full. Respects
stack_modto split stack. - Parameters:
slot(optional number),stack_mod(boolean).
Inventory:GetNumSlots()
- Description: Returns
maxslots. - Parameters: None.
Inventory:GetItemSlot(item)
- Description: Returns inventory slot key where
itemis stored (ornil). - Parameters:
item(Entity).
Inventory:IsHolding(item, checkcontainer)
- Description: Checks if
itemis active, equipped, or stored in inventory (optionally in open containers). - Parameters:
item(Entity),checkcontainer(boolean ornil).
Inventory:FindItem(fn)
- Description: Returns first item matching
fn(item)in inventory, equipment, active item, or overflow. - Parameters:
fn(function).
Inventory:FindItems(fn)
- Description: Returns all items matching
fn(item)across inventory slots, equipment, active item, and overflow. - Parameters:
fn(function).
Inventory:ForEachItem(fn, ...)
- Description: Runs
fn(item, ...)on all inventory items, equipped items, and active item (including overflow). - Parameters:
fn(function),...(arguments passed tofn).
Inventory:ForEachWetableItem(fn, ...)
- Description: Runs
fn(item, ...)on inventory, equipment, and active items (does not recurse into containers). - Parameters:
fn(function),...(arguments).
Inventory:ForEachEquipment(fn, ...)
- Description: Runs
fn(item, ...)on equipped items. - Parameters:
fn(function),...(arguments).
Inventory:ForEachItemSlot(fn, ...)
- Description: Runs
fn(item, ...)on items initemslots. - Parameters:
fn(function),...(arguments).
Inventory:RemoveItemBySlot(slot, keepoverstacked)
- Description: Removes and returns item from
slot, if present. - Parameters:
slot(number/string),keepoverstacked(optional boolean).
Inventory:DropItem(item, wholestack, randomdir, pos, keepoverstacked)
- Description: Drops item in the world. Updates position, triggers
"dropitem", and cleans up ownership. - Parameters:
item(Entity),wholestack(boolean),randomdir(boolean),pos(Vector3, optional),keepoverstacked(boolean).
Inventory:IsInsulated()
- Description: Returns
trueif any equipment is insulated (electrically) or if externally insulated. - Parameters: None.
Inventory:GetEquippedItem(eslot)
- Description: Returns item in equipment slot
eslot. - Parameters:
eslot(string, e.g.,"HANDS").
Inventory:GetItemInSlot(slot)
- Description: Returns item in inventory slot
slot. - Parameters:
slot(number).
Inventory:GetFirstItemInAnySlot()
- Description: Returns first non-nil item in
itemslots(ordered 1..maxslots). - Parameters: None.
Inventory:IsFull()
- Description: Returns
trueif all inventory slots are occupied. - Parameters: None.
Inventory:GetNextAvailableSlot(item)
- Description: Finds next available slot for
item, considering stacks, overflow, and prioritize container logic. Returnsslot, container_table. - Parameters:
item(Entity).
Inventory:CanAcceptCount(item, maxcount)
- Description: Computes how many of
item(up tomaxcount) can be accepted, including stacks and overflow. - Parameters:
item(Entity),maxcount(number, optional). - Returns: Acceptable count.
Inventory:GiveActiveItem(inst)
- Description: Sets
instas the active item. Ensures item is not already held or equipped, calls pickup hooks, and pushes"itemget". - Parameters:
inst(Entity withinventoryitem).
Inventory:GiveItem(inst, slot, src_pos)
- Description: Attempts to give item to inventory. Handles stacking, overflow, active item fallback, and fullness logic. Returns slot (or
true) if successful. - Parameters:
inst(Entity),slot(optional number),src_pos(Vector3, optional).
Inventory:Unequip(equipslot, slip, force)
- Description: Unequips item from
equipslot. Updatesheavylifting/floaterheld. Triggers"unequip"and set bonus updates. - Parameters:
equipslot(string),slip(boolean, optional),force(boolean, optional).
Inventory:SetActiveItem(item)
- Description: Sets
itemas active. If item cannot be held in container, drops it. Triggers"newactiveitem". - Parameters:
item(Entity ornil).
Inventory:Equip(item, old_to_active, no_animation, force_ui_anim)
- Description: Equips item to its defined slot. Handles conflicts (unequips old item, moves to overflow/active item), updates state flags (
heavylifting,floaterheld), triggers"equip", and updates set bonuses. - Parameters:
item(Entity),old_to_active(boolean),no_animation(boolean),force_ui_anim(boolean).
Inventory:RemoveItem(item, wholestack, checkallcontainers, keepoverstacked)
- Description: Removes
itemfrom inventory, equipment, active item, or overflow (and optionallyopencontainers). Returns removed item ornil. - Parameters:
item(Entity),wholestack(boolean),checkallcontainers(boolean),keepoverstacked(boolean).
Inventory:GetOverflowContainer()
- Description: Returns the container used as overflow (e.g., backpack in
BODYslot) ornil. - Parameters: None.
Inventory:Has(item, amount, checkallcontainers)
- Description: Counts how many items matching
item.prefabare present. Supportscheckallcontainers. - Parameters:
item(prefab string),amount(number),checkallcontainers(boolean). - Returns:
found >= amount,total_found.
Inventory:HasItemThatMatches(fn, amount)
- Description: Counts items satisfying custom predicate
fn(item). Supports overflow and containers. - Parameters:
fn(function),amount(number). - Returns:
found >= amount,total_found.
Inventory:HasItemWithTag(tag, amount)
- Description: Counts items with
tagin inventory, active, equipment, and overflow. - Parameters:
tag(string),amount(number). - Returns:
found >= amount,total_found.
Inventory:GetItemsWithTag(tag)
- Description: Returns list of all items with
tagin inventory, active, equipment, and overflow. - Parameters:
tag(string).
Inventory:GetItemByName(item, amount, checkallcontainers)
- Description: Returns table
{item_entity => count}of up toamountitems matchingitem(prefab) across inventory, active, overflow, and containers. - Parameters:
item(prefab string),amount(number),checkallcontainers(boolean).
Inventory:GetCraftingIngredient(item, amount)
- Description: Returns
{item_entity => count}of ingredients suitable for crafting, excluding"nocrafting"items. Prioritizes smaller stacks and considers crafting containers. - Parameters:
item(prefab string),amount(number).
Inventory:ConsumeByName(item, amount)
- Description: Destroys up to
amountitems matchingitemfrom inventory and overflow. - Parameters:
item(prefab string),amount(number).
Inventory:DropEverything(ondeath, keepequip)
- Description: Drops all items, respecting
keepondeath,curseditem, andondeathflags. Optionally keeps equipped items. - Parameters:
ondeath(boolean),keepequip(boolean).
Inventory:DropEquipped(keepBackpack, keepPreventUnequipping)
- Description: Drops all equipped items unless filtered by flags.
- Parameters:
keepBackpack(boolean),keepPreventUnequipping(boolean).
Inventory:DestroyContents(onpredestroyitemcallbackfn)
- Description: Recursively destroys all items (including containers), optionally calling callback per item.
- Parameters:
onpredestroyitemcallbackfn(function, optional).
Inventory:Show(), Inventory:Open(), Inventory:Hide(), Inventory:Close(keepactiveitem)
- Description: Manage inventory UI visibility and open state.
Open/Closehandle overflow container states and HUD updates. - Parameters: None, except
keepactiveiteminClose.
Inventory:CloseAllChestContainers()
- Description: Closes all chest-type containers in
opencontainers.
Inventory UI Action Handlers (e.g., PutOneOfActiveItemInSlot, SwapActiveItemWithSlot, TakeActiveItemFromEquipSlot, MoveItemFromAllOfSlot, etc.)
- Description: Handle UI interactions (clicks, drags) for manipulating items within inventory slots and between inventory and containers. Many use internal flags (
ignoresound,silentfull) and honor constraints like stackability andcanonlygoinpocket.
Inventory:CanAccessItem(item)
- Description: Checks if item is currently accessible (inventory visible, item owned or in an open non-readonly container).
- Parameters:
item(Entity).
Inventory:UseItemFromInvTile(...), Inventory:ControllerUseItemOnItemFromInvTile(...), Inventory:ControllerUseItemOnSelfFromInvTile(...), Inventory:ControllerUseItemOnSceneFromInvTile(...)
- Description: Handle remote/remote-predicted actions initiated from inventory UI tiles. Creates and pushes
BufferedActions viaplayeractionpickerorplayercontroller.
Inventory:InspectItemFromInvTile(item)
- Description: Initiates
"LOOKAT"action onitemvia locomotor. - Parameters:
item(Entity).
Inventory:DropItemFromInvTile(item, single)
- Description: Initiates
"DROP"action with optionswholestackandinstant. - Parameters:
item(Entity),single(boolean).
Inventory:CastSpellBookFromInv(item, spell_id)
- Description: Initiates
"CAST_SPELLBOOK"action foritemspellbook, optionally selectingspell_id. - Parameters:
item(Entity),spell_id(optional number).
Inventory:EquipActiveItem(), Inventory:EquipActionItem(item), Inventory:SwapEquipWithActiveItem(), Inventory:TakeActiveItemFromEquipSlot(eslot)
- Description: Convenience wrappers for common equipment swaps and transitions from active item ↔ equipment.
Inventory:MoveItemFromAllOfSlot(slot, container), MoveItemFromHalfOfSlot(...), MoveItemFromCountOfSlot(...)
- Description: Transfer items from inventory slots to an open container (e.g., crafting), respecting container state and slot mapping.
Inventory:GetEquippedMoistureRate(slot)
- Description: Returns total
moisture, maxmoisture rate from equipment (or specificslot). - Parameters:
slot(optional string).
Inventory:GetWaterproofness(slot)
- Description: Returns total waterproof rating (0+) from equipment or specific slot.
- Parameters:
slot(optional string).
Inventory:IsWaterproof()
- Description: Returns
trueif total waterproof rating ≥ 1. - Parameters: None.
Inventory:TransferComponent(newinst)
- Description: Transfers inventory contents to another entity (e.g., after rebirth) using
EmptyBeardthenTransferInventory. - Parameters:
newinst(Entity).
Inventory:GetOpenContainerProxyFor(master)
- Description: Returns the container proxy instance associated with
mastercontainer. - Parameters:
master(Entity).
Events & Listeners
- Listens for
"death"on owner → callsOnDeath, which triggersDropEverything(true). - Listens for
"player_despawn"on owner → callsOnOwnerDespawned, sending"player_despawn"to all items initemslots,equipslots, andactiveitem. - Pushes the following events:
"dropitem"(after dropping)."gotnewitem"(after giving new item)."itemget"(after item placed in inventory slot)."itemlose"(after item removed from inventory slot or active item)."unequip"(after unequipping)."equip"(after equipping)."newactiveitem"(after setting active item)."inventoryfull"(when item cannot be given and triggers a message)."setoverflow"(after equipping an overflow container inBODYslot).