This page documents the Trade userdata returned by the trading component. Each object represents one specific merchant recipe from one merchant currently in range.
- Repository:
opencomputers - Runtime type:
Tradeuserdata - Created by:
trading.getTrades()
- One merchant
- One recipe index belonging to that merchant
- One live trading opportunity that may later become invalid if the merchant moves away, dies, or disables the recipe
- Syntax:
trade.getMerchantId() - Returns:
number - Purpose: Returns the merchant grouping id assigned by the current
getTrades()scan.
This id is only a sort/group helper inside the scan result set. It is not a permanent world identifier.
- Syntax:
trade.getInput() - Returns:
firstStack, secondStack - Purpose: Returns the input items the merchant wants for this recipe.
Behavior:
firstStackis always the primary cost.secondStackisnilif the recipe only has one required input.
- Syntax:
trade.getOutput() - Returns:
table - Purpose: Returns the item stack the merchant offers.
- Syntax:
trade.isEnabled() - Returns:
boolean - Purpose: Returns whether the merchant and recipe are still currently usable.
This becomes false if the merchant disappears or the recipe becomes disabled.
- Syntax:
trade.trade() - Returns:
- Success:
true - Failure:
false, reason
- Success:
- Purpose: Attempts to complete the trade using the host's main inventory.
Possible failure reasons:
trading requires an inventory upgrade to be installedtrade has become invalidout of rangetrade is disablednot enough inventory space to tradenot enough items to trade
Behavior details:
- The host must have an accessible main inventory.
- The output item must fit before the trade is committed.
- The implementation first tries exact inventory extraction, then retries with looser matching if needed.
local component = require("component")
local trading = component.trading
for _, offer in ipairs(trading.getTrades()) do
if offer.isEnabled() then
local ok, reason = offer.trade()
print("Trade result:", ok, reason)
end
endtradingrobotinventory_controller