This page documents the inventory-selection callbacks exposed by inventory-aware hosts such as the robot component. These callbacks work on the device's own internal inventory, not on adjacent inventories.
- Repository:
opencomputers - Typical Lua component names: usually part of
robotand similar inventory-owning hosts - Backing trait:
InventoryControl
Lua-facing slot numbers are 1-based.
- Slot
1is the first internal slot. - Passing an out-of-range slot raises
invalid slot.
- Syntax:
device.inventorySize() - Returns:
number - Purpose: Returns the size of the host's internal inventory.
- Syntax:
device.select([slot]) - Returns:
number - Purpose: Reads or changes the currently selected slot.
When called without an argument, it returns the current selected slot. When a slot is provided, it becomes the new selected slot.
- Syntax:
device.count([slot]) - Returns:
number - Purpose: Returns the stack size in the specified slot, or in the selected slot if omitted.
Empty slots return 0.
- Syntax:
device.space([slot]) - Returns:
number - Purpose: Returns the remaining item capacity in the specified slot, or in the selected slot if omitted.
For empty slots, this usually returns the inventory stack limit.
- Syntax:
device.compareTo(otherSlot[, checkNBT]) - Parameters:
otherSlot:number: slot to compare against.checkNBT:boolean: whentrue, includes NBT in the comparison.
- Returns:
boolean - Purpose: Compares the selected slot with another internal slot.
Behavior:
- Two empty slots compare as
true. - One empty and one non-empty slot compare as
false.
- Syntax:
device.transferTo(toSlot[, amount]) - Parameters:
toSlot:number: destination slot inside the same inventory.amount:number: maximum item count to move, clamped to0..64.
- Returns:
boolean - Purpose: Moves items from the selected slot into another internal slot.
Behavior details:
- Returns
trueimmediately iftoSlotis the selected slot oramountis0. - Stacks matching items when possible.
- Swaps stacks only when the full source stack is allowed to move.
- Returns
falsewhen the move cannot be completed.
local robot = require("robot")
print("Inventory size:", robot.inventorySize())
print("Selected slot:", robot.select())
robot.select(1)
print("Items in slot 1:", robot.count())
print("Free space in slot 1:", robot.space())
print("Slot 1 equals slot 2:", robot.compareTo(2, true))
local moved = robot.transferTo(2, 8)
print("Moved items:", moved)opencomputers-inventory-analyticsopencomputers-inventory-world-controlrobot