The crafting component provides the robot crafting upgrade API. It lets a robot craft items from the 3 x 3 crafting grid mapped onto the top-left portion of the robot inventory.
This component is intentionally small, but its behavior matters: it repeatedly crafts the same recipe until the requested amount is reached, no recipe is available anymore, or the output would change.
- Repository:
opencomputers - Component name:
crafting - Typical host: robot with a crafting upgrade installed
local component = require("component")
local crafting = component.crafting
if not crafting then
error("crafting upgrade not installed")
endThe crafting upgrade reads the robot inventory as a 3 x 3 crafting matrix using the top-left part of the inventory:
slot 1 slot 2 slot 3
slot 5 slot 6 slot 7
slot 9 slot 10 slot 11
In other words, each crafting row uses the first three slots of a four-slot robot inventory row.
- Syntax:
crafting.craft([count]) - Returns:
boolean, number - Purpose: Repeatedly crafts the current recipe from the robot crafting grid.
Parameters:
count:numberoptional: desired number of output items. Defaults to64.
Behavior:
- The requested count is clamped to the range
0through64. - The component first determines the recipe currently present in the grid.
- It keeps crafting while:
- enough ingredients remain,
- the recipe is still valid,
- the produced output item stays the same.
- Container items and crafting by-products are placed back into the robot inventory when possible.
Return values:
- First result:
truewhen a valid recipe existed, otherwisefalse. - Second result: total number of output items actually crafted.
Examples:
Craft as many items as possible up to a full stack:
local ok, crafted = crafting.craft()
if ok then
print("Crafted items:", crafted)
else
print("No valid recipe in the crafting grid")
endCraft exactly one recipe result when possible:
local ok, crafted = crafting.craft(1)
print("Recipe found:", ok)
print("Items crafted:", crafted)inventory_controllerrobotcomponent