Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
component:component_access:zh [2022/08/11 09:20]
fight_xing [Signals]
component:component_access:zh [2023/12/27 04:26] (current)
hfsr [地址]
Line 1: Line 1:
-Component Access +访问组件 
-================+======== 
 +本页描述了如何通过Lua访问[[:​component:​zh|组件 API]]。简单来说,组件是一些方块或物品,它们提供了一些API,供与之相连的电脑上的Lua程序调用。
  
-**本篇文章部分/​全部内容没有进行翻译**+地址 
 +---- 
 +每个组件都有一个地址。这个地址是一个UUID,即唯一标识符。大多数情况下都可以简写这些地址。要从一个缩写地址中获得完整的地址,你可以调用`component.get`。你可以手持[[item:​analyzer:​zh|分析仪]]按住ctrl右击方块以得到任意方块的地址
  
-This page describes how to get access to a [[:​component|component'​s API]] from Lua. To recap: components are blocks or items that provide some API to Lua programs running on computers connected to said component. +要获得所有连接到你计算机的组件列表,你可以这样做:
- +
-Addresses +
---------- +
-Components all have an address. This address is a UUID, a unique identifier. In most cases it is OK to abbreviate these addresses. For example to get the full address from an abbreviated one you can use `component.get`. To get the address of any block in particular, you can use the [[item:​analyzer|Analyzer]] by holding the control key while right clicking the block. +
- +
-To get a list of all components attached to your computer you can do this:+
 ```lua ```lua
 Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
Line 21: Line 18:
 ``` ```
  
-Note that item components will usually keep their address even when removed from a computer. So when removing a hard drive and inserting it back in, it will have the same address as before. This is *notthe case for block components, they will always get a new address after being broken and placed again.+注意,即使从计算机上拆下来,物品组件的地址通常也不会改变。因此,如果你移除一个硬盘再将其重新插入,它的地址仍与之前的相同。而方块组件则**不是**这样,拆除它们再重新安装必然会改变地址。
  
-Optionally you can filter components list like this:+你可以选择像这样过滤组件列表:
  
-This code will add every component which name contains ​"​adar" ​such as computronics ​radar or warpDrive ​radar to the local radars table+这段代码会将每一个名称中包含 ​"​adar" ​的组件添加到表radars中,如computronics的雷达(computronicsRadar)或warpDrive(曲率驱动)的雷达(warpdriveRadar)。
 ```lua ```lua
 local component = require("​component"​) local component = require("​component"​)
Line 34: Line 31:
 ``` ```
  
-This code will add all connected warpDrive ​radars ​only to the local radars table+这段代码只将所有连接的曲率驱动雷达(warpdriveRadar)添加到表 ​radars ​
 ```lua ```lua
 local component = require("​component"​) local component = require("​component"​)
Line 43: Line 40:
 ``` ```
  
-Primary Components +首选组件 
------------------- +------ 
-The [[api:​component|component API]] keeps track of one component of each type as a "​primary"​ component. If there are multiple components of the same type, which one will be the primary component is random. You can access the primary component of a specific type via `component.xxx`, where `xxx` is the type. For example:+[[api:​component:zh|component(组件) ​API]]会持续追踪每种组件类型的“首选”组件。如果同一种类型下有多个组件,则会随机选择一个作为首选组件。你可以通过`component.xxx`访问特定类型的首选组件,其中`xxx`是类型,比如:
 ```lua ```lua
 lua> =component.gpu.address lua> =component.gpu.address
Line 51: Line 48:
 ``` ```
  
-Alternatively you can use `component.getPrimary("​xxx"​)`. Just keep in mind that this will throw an error if there is no primary component for the specified type, so you may wish to check that with `component.isAvailable("​xxx"​)` ​first. Having it throw an error is usually clearer than getting a nil dereference later on.+或者你也可以使用`component.getPrimary("​xxx"​)`。切记,如果指定类型的首选组件不存在,调用此方法会抛出一个错误,因此最好先用`component.isAvailable("​xxx"​)`进行检查。之所以选择直接抛出错误而不是返回`nil`,是因为相较于在后续代码中试图访问`nil`中的元素而产生错误,直接在这里抛出一个错误更能明确问题来源。
  
-This system is in mainly place to allow the OS to pick the screen to initially output to.+这个系统主要是为了让操作系统选择初始输出屏幕。
  
-Proxies +代理对象 
-------- +---- 
-So now that we know how to get the address of components, let's see how we can interact with them. There are two ways to go about this. One is to call `component.invoke(address,​ method, ...)`. For example:+现在我们已经知道了如何获得组件地址,那么看看如何与它们交互吧。有两种方法可以做到这一点。一种是调用`component.invoke(address,​ method, ...)`。例如:
 ```lua ```lua
 local component = require("​component"​) local component = require("​component"​)
Line 63: Line 60:
 ``` ```
  
-The preferred way will usually be to get a proxy, however. A proxy is simply a table that provides one function for each API callback, named after that callback. In addition, each proxy has two fields: ​`address`, which holds the address of the wrapped component, and `type`, which holds the type of the component. You can get a proxy like this:+通常而言,更好的方法是获取一个代理对象。代理对象是一个简单的表,为各个API回调提供同名的的函数。此外,每个代理都有两个字段:`address`,用来保存被包装的组件的地址。`type`,用来保存组件的类型。你可以通过这种方法获得代理对象:
 ```lua ```lua
 local component = require("​component"​) local component = require("​component"​)
 local proxy = component.proxy(address) local proxy = component.proxy(address)
  
--- The call above using the proxy:+-- 上文调用的使用代理对象版本:
 proxy.broadcast(port,​ data) proxy.broadcast(port,​ data)
  
--- The common proxy fields: +-- 常见的代理对象字段: 
-print(proxy.address) -- address passed to component.proxy ​above+print(proxy.address) -- 传给上述component.proxy的地址
 print(proxy.type) -- "​modem"​ print(proxy.type) -- "​modem"​
 ``` ```
  
-Note that each primary component you access via `component.getPrimary` ​or `component.xxx` ​is in fact a proxy.+请注意,通过`component.getPrimary``component.xxx`访问的首选组件实际上都是代理对象。
  
-Direct Calls +直接调用 
------------- +-------- 
-Some component callbacks can be performed as "direct calls". Direct calls are performed in the computer'​s worker thread, meaning they will return instantly. Normal calls are delegated to the main server thread, to avoid race conditions and other threading issues, which also means that normal calls will take up to one tick (i.e. 50 ms). Just to be clear: this only applies to component APIs.+一些组件的回调可以以"直接调用"的形式执行。直接调用是在计算机的工作线程中进行的,这意味着它们会立即返回。普通调用会被委托给主服务端线程,以避免出现竞态问题或其他线程问题,这也意味着普通调用至多会需要一tick(即50ms)才能完成。这里明确一下:只有component(组件API会有这种情况。
  
-Signals +信号 
-------- +---- 
-An important part of interacting with components are [[:​component:​signals|Signals]]. These can be queued by components to notify computers of external changes and events. For example, user input is forwarded to computers via signals. Computers can also queue their own signals, which can be useful for code-reuse, or just notifying other parts of your code asynchronously.+[[:​component:​signals:zh|信号]]是于组件交互时很重要的一部分。信号可以被组件推入队列,以通知电脑外部变化和事件。例如,用户的输入就是通过信号转发给电脑的。电脑也可以将自己的信号加入队列,这有助于代码复用,或者只是异步通知你代码的其他部分。
  
 目录 目录
------------+----
 {{page>​component:​contents:​zh&​noheader&​noeditbutton&​nouser&​nofooter}} {{page>​component:​contents:​zh&​noheader&​noeditbutton&​nouser&​nofooter}}
 +