-
Notifications
You must be signed in to change notification settings - Fork 435
API Component
Vexatos edited this page Jul 13, 2014
·
6 revisions
For those that don't like images: the wiki has moved to a new place, http://ocdoc.cil.li/.
This wiki will no longer be updated.
The component API is used to access and interact with components available to a computer. Also see the page on component interaction.
-
component.doc(address:string, method:string): string
Returns the documentation string for the method with the specified name of the component with the specified address, if any. Note that you can also get this string by usingtostring
on a method in a proxy, for exampletostring(component.screen.isOn)
. -
component.invoke(address:string, method:string[, ...]): ...
Calls the method with the specified name on the component with the specified address, passing the remaining arguments as arguments to that method. Returns the result of the method call, i.e. the values returned by the method. Depending on the called method's implementation this may throw. -
component.list([filter:string]):function
Returns an iterator over all components currently attached to the computer, providing tuples of address and component type. Use it like so:for address, componentType in component.list() do ... end
Iffilter
is set this will only return components that contain the filter string (this is not a pattern/regular expression). For example,component.list("red")
will returnredstone
components. -
component.proxy(address:string):table
Gets a 'proxy' object for a component that provides all methods the component provides as fields, so they can be called more directly (instead of viainvoke
). This is what's used to generate 'primaries' of the individual component types, i.e. what you get viacomponent.blah
.
For example, you can use it like so:component.proxy(component.list("redstone")()).getInput(sides.north)
, which gets you a proxy for the firstredstone
component returned by thecomponent.list
iterator, and then callsgetInput
on it.
Note that proxies will always have at least two fields,type
with the component's type name, andaddress
with the component's address. -
component.type(address:string):string
Get the component type of the component with the specified address. -
component.get(address: string[, componentType: string]): string | (nil, string)
Tries to resolve an abbreviated address to a full address. Returns the full address on success, ornil
and an error message otherwise. Optionally filters by component type. -
component.isAvailable(componentType: string): boolean
Checks if there is a primary component of the specified component type. -
component.getPrimary(componentType: string): table
Gets the proxy for the primary component of the specified type. Throws an error if there is no primary component of the specified type. -
component.setPrimary(componentType: string, address: string)
Sets a new primary component for the specified component type. The address may be abbreviated, but must be valid if it is notnil
. Triggers thecomponent_unavailable
andcomponent_available
signals if set tonil
or a new value, respectively. -
component.list([filter: string]): function
Returns an iterator which returns pairs ofaddress, type
for each component connected to the computer. It optionally takes a filter - if specified this will only return those components for which the filter is a substring of the component type. For example:
local component = require("component")
-- prints `redstone` for each attached redstone card.
for _, name in component.list("red") do print(name) end
Note that the component API has a metatable that allows the following syntax:
local component = require("component")
local rs0 = component.getPrimary("redstone")
local rs1 = component.redstone -- syntactic sugar
print(rs0 == rs1) -- true