API/component: Difference between revisions
Jump to navigation
Jump to search
imported>OCDoc Import Imported from legacy OpenComputers documentation at ocdoc.cil.li |
Clean up Markdown artifacts from DokuWiki migration |
||
| Line 3: | Line 3: | ||
The component API is used to access and interact with components available to a computer. Also see [[Component/component access|the page on component interaction]]. | The component API is used to access and interact with components available to a computer. Also see [[Component/component access|the page on component interaction]]. | ||
* | * <code>component.doc(address:string, method:string): string</code> | ||
<pre>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 using | <pre>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 using tostring on a method in a proxy, for example tostring(component.screen.isOn). | ||
</pre> | </pre> | ||
* | * <code>component.invoke(address:string, method:string[, ...]): ...</code> | ||
<pre>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. | <pre>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. | ||
</pre> | </pre> | ||
* | * <code>component.list([filter:string[, exact:boolean]]):function</code> | ||
<pre>Returns a table with all components currently attached to the computer, with address as a key and component type as a value. It also provides iterator syntax via | <pre>Returns a table with all components currently attached to the computer, with address as a key and component type as a value. It also provides iterator syntax via __call, so you can use it like so: for address, componentType in component.list() do ... end | ||
If | If filter 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 return redstone components. | ||
If | If true is passed as a second parameter, exact matching is enforced, e.g. red will ''not'' match redstone. | ||
</pre> | </pre> | ||
* | * <code>component.methods(address:string):table</code> | ||
<pre>Returns a table with the names of all methods provided by the component with the specified address. The names are the keys in the table, the values indicate whether the method is called directly or not. | <pre>Returns a table with the names of all methods provided by the component with the specified address. The names are the keys in the table, the values indicate whether the method is called directly or not. | ||
</pre> | </pre> | ||
* | * <code>component.proxy(address:string):table</code> | ||
<pre>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 via | <pre>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 via invoke). This is what's used to generate 'primaries' of the individual component types, i.e. what you get via component.blah. | ||
For example, you can use it like so: | For example, you can use it like so: component.proxy(component.list("redstone")()).getInput(sides.north), which gets you a proxy for the first redstone component returned by the component.list iterator, and then calls getInput on it. | ||
Note that proxies will always have at least two fields, | Note that proxies will always have at least two fields, type with the component's type name, and address with the component's address. | ||
</pre> | </pre> | ||
* | * <code>component.type(address:string):string</code> | ||
<pre>Get the component type of the component with the specified address. | <pre>Get the component type of the component with the specified address. | ||
</pre> | </pre> | ||
* | * <code>component.slot(address:string):string</code> | ||
<pre>Return slot number which the component is installed into. Returns -1 if it doesn't otherwise make sense. | <pre>Return slot number which the component is installed into. Returns -1 if it doesn't otherwise make sense. | ||
</pre> | </pre> | ||
* | * <code>component.fields(address:string):string</code> | ||
<pre>Undocumented | <pre>Undocumented | ||
</pre> | </pre> | ||
* | * <code>component.get(address: string[, componentType: string]): string | (nil, string)</code> | ||
<pre>Tries to resolve an abbreviated address to a full address. Returns the full address on success, or | <pre>Tries to resolve an abbreviated address to a full address. Returns the full address on success, or nil and an error message otherwise. Optionally filters by component type. | ||
</pre> | </pre> | ||
* | * <code>component.isAvailable(componentType: string): boolean</code> | ||
<pre>Checks if there is a primary component of the specified component type. | <pre>Checks if there is a primary component of the specified component type. | ||
</pre> | </pre> | ||
* | * <code>component.getPrimary(componentType: string): table</code> | ||
<pre>Gets the proxy for the primary component of the specified type. Throws an error if there is no primary component of the specified type. | <pre>Gets the proxy for the primary component of the specified type. Throws an error if there is no primary component of the specified type. | ||
</pre> | </pre> | ||
* | * <code>component.setPrimary(componentType: string, address: string)</code> | ||
<pre>Sets a new primary component for the specified component type. The address may be abbreviated, but must be valid if it is not | <pre>Sets a new primary component for the specified component type. The address may be abbreviated, but must be valid if it is not nil. Triggers the component_unavailable and component_available signals if set to nil or a new value, respectively. | ||
</pre> | </pre> | ||
Note that the component API has a metatable that allows the following syntax: | Note that the component API has a metatable that allows the following syntax: | ||
<syntaxhighlight lang="lua"> | |||
local component = require("component") local rs0 = component.getPrimary("redstone") local rs1 = component.redstone -- syntactic sugar print(rs0 == rs1) -- true | |||
</syntaxhighlight> | |||
== Contents == | == Contents == | ||
{{:API/contents}} | {{:API/contents}} | ||
Latest revision as of 20:26, 24 August 2026
Component API
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 using tostring on a method in a proxy, for example tostring(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[, exact:boolean]]):function
Returns a table with all components currently attached to the computer, with address as a key and component type as a value. It also provides iterator syntax via __call, so you can use it like so: for address, componentType in component.list() do ... end
If filter 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 return redstone components.
If true is passed as a second parameter, exact matching is enforced, e.g. red will ''not'' match redstone.
component.methods(address:string):table
Returns a table with the names of all methods provided by the component with the specified address. The names are the keys in the table, the values indicate whether the method is called directly or not.
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 via invoke). This is what's used to generate 'primaries' of the individual component types, i.e. what you get via component.blah.
For example, you can use it like so: component.proxy(component.list("redstone")()).getInput(sides.north), which gets you a proxy for the first redstone component returned by the component.list iterator, and then calls getInput on it.
Note that proxies will always have at least two fields, type with the component's type name, and address with the component's address.
component.type(address:string):string
Get the component type of the component with the specified address.
component.slot(address:string):string
Return slot number which the component is installed into. Returns -1 if it doesn't otherwise make sense.
component.fields(address:string):string
Undocumented
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, or nil 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 not nil. Triggers the component_unavailable and component_available signals if set to nil or a new value, respectively.
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) -- trueContents
| APIs | |
|---|---|
| OpenOS | buffer - colors - component - computer - event - filesystem - uuid - internet - keyboard - note - process - rc - robot - serialization - shell - sides - term - text - thread - transforms - unicode |
| Lua Libraries | coroutine - package - io - os |