Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
api:component:zh [2022/08/11 10:26]
fight_xing created
api:component:zh [2023/11/29 06:21] (current)
hfsr [Component(组件) API]
Line 1: Line 1:
-Component API+Component(组件) ​API
 ============= =============
- +component(组件) ​API可用于访问电脑中的组件并与其交互。读者还可查看[[:​component:​component_access:zh|有关访问组件的页面]]
-**本篇文章的部分/​全部内容还没有进行翻译。** +
- +
-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]].+
  
 - `component.doc(address:​string,​ method:​string):​ string`  ​ - `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)`.+  ​返回指定组件中指定方法的对应文档字符串(若存在)。请注意,你也可以对代理对象的方法使用`tostring`来获取此字符串。例如`tostring(component.screen.isOn)`。   
 +\\
 - `component.invoke(address:​string,​ method:​string[,​ ...]): ...`  ​ - `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`  ​ - `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`   +  ​返回一个表,其中列出当前所有连接到电脑的组件,表中以组件地址为键并以组件类型为值。此方法还通过`__call`提供了迭代器语法,所以你还可以这样调用:`for address, componentType in component.list() do ... end`   
-  ​If `filter` ​is set this will only return components that contain the filter string (this is *nota pattern/regular expression). For example, ​`component.list("​red"​)` ​will return ​`redstone` ​components. ​  +  ​若设置了`filter`(过滤器),则此方法只会返回包含过滤器字符串(**并非**模式字符串/正则表达式)的组件。例如,`component.list("​red"​)`将会返回`redstone`组件的结果。 ​  
-  ​If `true` ​is passed as a second parameter, exact matching is enforced, e.g. `red` will *notmatch `redstone`.+  ​如果将`true`作为第二个参数传递,则执行精确匹配。例如`red`在此情况下**不会**匹配`redstone`。   
 +\\
 - `component.methods(address:​string):​table`  ​ - `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`  ​ - `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`  +  ​获取某组件的“proxy”对象(代理对象),代理对象将组件提供的所有方法以字段的形式给出,因此可以更直接地调用它们(无需通过`invoke`)。这也是用来生成各个组件类型的“首选组件”的方法,也即你通过`component.某某`(“某某”为组件类型)方法得到的东西。 ​  
-  ​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.   +  ​例如,你可以这样使用此方法:`component.proxy(component.list("​redstone"​)()).getInput(sides.north)`,这段代码会为你获取`component.list`迭代器返回的第一个`redstone`组件的代理对象,然后调用它的`getInput`方法。 ​  
-  ​Note that proxies will always have at least two fields, ​`type` ​with the component'​s type name, and `address` ​with the component'​s address.+  ​请注意代理对象无论如何都至少有两个字段:一是`type`与组件的类型名,二是`address`与组件的地址。 ​  
 +\\
 - `component.type(address:​string):​string`  ​ - `component.type(address:​string):​string`  ​
-  ​Get the component type of the component with the specified address.+  ​获取指定组件的类型。 ​  
 +\\
 - `component.slot(address:​string):​string`  ​ - `component.slot(address:​string):​string`  ​
-  ​Return slot number which the component is installed into. Returns ​-1 if it doesn'​t otherwise make sense.+  ​返回安装组件的插槽编号。若无意义则返回-1。   
 +\\
 - `component.fields(address:​string):​string`  ​ - `component.fields(address:​string):​string`  ​
-  ​Undocumented+  ​未记录 ​  
 +\\
 - `component.get(address:​ string[, componentType:​ string]): string | (nil, string)`  ​ - `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.+  ​尝试将简写地址(部分地址)解析为完整地址。成功时返回完整地址,失败时返回`nil`和一条报错信息。可选是否通过组件类型进行筛选。 ​  
 +\\
 - `component.isAvailable(componentType:​ string): boolean`  ​ - `component.isAvailable(componentType:​ string): boolean`  ​
-  ​Checks if there is a primary component of the specified component type.+  ​检查指定的组件类型是否存在首选组件。 ​  
 +\\
 - `component.getPrimary(componentType:​ string): table`  ​ - `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)`  ​ - `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. +  ​为指定组件类型设定一个新的首选组件。地址可以为简写地址,但必须合法,否则应当为`nil`。将其设定为`nil`和新值时分别会触发`component_unavailable``component_available`信号。 ​  
-   +\\ 
-Note that the component API has a metatable that allows the following syntax:+ 
 +请注意component API有一个元表,支持以下语法:
  
 ```lua ```lua
 local component = require("​component"​) local component = require("​component"​)
 local rs0 = component.getPrimary("​redstone"​) local rs0 = component.getPrimary("​redstone"​)
-local rs1 = component.redstone -- syntactic sugar +local rs1 = component.redstone --语法糖 
-print(rs0 == rs1) -- true+print(rs0 == rs1) --输出值为true
 ``` ```