Inventoryitemholder
Based on game build 722832 | Last updated: 2026-04-28
Overview
InventoryItemHolder manages a single held item that can be given to or taken from an entity at any time. It handles item stacking validation, parent-child relationships, and event tracking for item lifecycle changes. The held item is removed from the scene and attached as a child to the holder entity. Does not support perishable items. The item drops when the structure finishes burning.
Usage example
local inst = CreateEntity()
inst:AddComponent("inventoryitemholder")
-- Configure what items can be given
inst.components.inventoryitemholder:SetAllowedTags({ "food", "seed" })
inst.components.inventoryitemholder:SetAcceptStacks(true)
-- Set callbacks for item transactions
inst.components.inventoryitemholder:SetOnItemGivenFn(function(holder, item, giver)
print("Item given:", item.prefab)
end)
-- Give an item to the holder
local item = SpawnPrefab("carrot")
inst.components.inventoryitemholder:GiveItem(item, player)
-- Take the item back
inst.components.inventoryitemholder:TakeItem(player)
Dependencies & tags
Components used:
inventory— used for DropItem and GiveItem operations on giver/taker entitiesinventoryitem— used for GetGrandOwner, HibernateLivingItem, InheritWorldWetnessAtTarget, OnDroppedstackable— used for CanStackWith, Get, IsFull, Put when handling stacked items
Tags:
inventoryitemholder_take— added when holding an item (entity can be taken from)inventoryitemholder_give— added when entity can accept items (removed when holding non-stackable or full stack)
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
item | entity | nil | The currently held item entity. Assignment triggers onitem watcher. |
allowed_tags | table | nil | List of tags that items must have to be accepted. nil means no restrictions. |
acceptstacks | boolean | false | Whether the holder accepts stacking items onto an existing held item. Assignment triggers onacceptstacks watcher. |
onitemgivenfn | function | nil | Callback fired when an item is successfully given. Signature: fn(holder, item, giver). |
onitemtakenfn | function | nil | Callback fired when an item is successfully taken. Signature: fn(holder, item, taker, was_full_stack). |
_onitemremoved | function | — | Internal callback that clears self.item when the held item is removed. |
_onitemmissing | function | — | Internal callback that handles cleanup when the held item is dropped or put in inventory. |
Main functions
SetAllowedTags(tags)
- Description: Sets the list of tags that items must have to be accepted by this holder.
- Parameters:
tags-- table of string tag names, ornilfor no restrictions. - Returns: nil
- Error states: None
SetOnItemGivenFn(fn)
- Description: Sets the callback function to fire when an item is successfully given to the holder.
- Parameters:
fn-- function with signaturefn(holder, item, giver). Note:itemmay benilif it was stacked onto an existing held item. - Returns: nil
- Error states: None
SetOnItemTakenFn(fn)
- Description: Sets the callback function to fire when an item is successfully taken from the holder.
- Parameters:
fn-- function with signaturefn(holder, item, taker, was_full_stack). Note:itemmay be invalid if it was stacked. - Returns: nil
- Error states: None
SetAcceptStacks(bool)
- Description: Configures whether the holder accepts stacking items onto an existing held item.
- Parameters:
bool-- boolean value (converted totrue/falseexplicitly). - Returns: nil
- Error states: None
IsHolding()
- Description: Returns whether the holder currently has an item.
- Parameters: None
- Returns:
trueifself.itemis notnil,falseotherwise. - Error states: None
CanGive(item, giver)
- Description: Checks if an item can be given to this holder. Validates inventoryitem component, allowed tags, and stacking rules.
- Parameters:
item-- entity to checkgiver-- entity attempting to give (not used in validation)
- Returns:
trueif item can be given,falseotherwise. - Error states: Errors if
itemhas noinventoryitemcomponent (nil dereference onitem.components.inventoryitem— no guard present).
CanTake(taker)
- Description: Checks if there is a valid item that can be taken from this holder.
- Parameters:
taker-- entity attempting to take (not used in validation). - Returns:
trueifself.itemexists and is valid,falseotherwise. - Error states: None
GiveItem(item, giver)
- Description: Attempts to give an item to this holder. Handles owner inventory dropping, stacking onto existing held items, and event registration for item lifecycle tracking.
- Parameters:
item-- entity to givegiver-- entity giving the item
- Returns:
trueif successful,falseif validation fails. - Error states: Errors if
giverhas noinventorycomponent when dropping from owner inventory (nil dereference onowner.components.inventory— no guard present beyond owner check). Errors ifitemhas noinventoryitemcomponent (checked in CanGive but accessed again without re-validation).
TakeItem(taker, wholestack)
- Description: Takes the held item and gives it to the taker's inventory or drops it at the holder's position. Handles partial stack extraction if
wholestackis false. - Parameters:
taker-- entity to receive the item (optional)wholestack-- boolean, defaults totrue. Iffalseand item is stackable, extracts one item from the stack.
- Returns:
trueif successful,falseif no valid item to take. - Error states: Errors if held
itemhas noinventoryitemcomponent (nil dereference onitem.components.inventoryitem:InheritWorldWetnessAtTarget— no guard present). Errors iftakeris valid but has noinventorycomponent (nil dereference ontaker.components.inventory:GiveItem— guarded bytaker.components.inventory ~= nilcheck).
OnSave()
- Description: Serializes the held item for world save. Returns save data table and references if item persists.
- Parameters: None
- Returns: Save data table and references, or
nilif no item or item doesn't persist. - Error states: None
OnLoad(data, newents)
- Description: Restores the held item from save data by spawning the saved record and giving it to the holder.
- Parameters:
data-- save data table from OnSavenewents— entity mapping for save record spawning
- Returns: None
- Error states: None
OnRemoveFromEntity()
- Description: Cleanup handler called when the component is removed from an entity. Takes the held item and removes holder tags.
- Parameters: None
- Returns: None
- Error states: None
GetDebugString()
- Description: Returns a debug string showing the held item and allowed tags for console inspection.
- Parameters: None
- Returns: Formatted string with item and allowed tags info.
- Error states: None
onitem(self, item) (local)
- Description: Property watcher callback fired when
self.itemis assigned. Updates holder tags based on whether an item is held and stacking configuration. - Parameters:
self-- component instanceitem-- the new item value (entity ornil)
- Returns: None
- Error states: None
onacceptstacks(self, acceptstacks) (local)
- Description: Property watcher callback fired when
self.acceptstacksis assigned. Addsinventoryitemholder_givetag if stacks are accepted. - Parameters:
self-- component instanceacceptstacks-- the new boolean value
- Returns: None
- Error states: None
_onitemremoved(item) (local)
- Description: Internal callback that clears
self.itemwhen the held item is removed from the world. - Parameters:
item-- the removed item entity. - Returns: None
- Error states: None
_onitemmissing(item) (local)
- Description: Internal callback that handles cleanup when the held item is dropped or put in inventory unexpectedly. Removes event listeners, clears tags, and fires
onitemtakenfncallback. - Parameters:
item-- the item that went missing. - Returns: None
- Error states: None
Events & listeners
- Listens to:
onremove— triggers_onitemremoved; clearsself.itemwhen held item is removed from worldondropped— triggers_onitemmissing; handles cleanup when held item is droppedonputininventory— triggers_onitemmissing; handles cleanup when held item is put in inventory