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
playground:playground:zh [2022/10/21 07:57]
pointtttt
playground:playground:zh [2023/11/29 05:21] (current)
hfsr
Line 1: Line 1:
-组件访问 
-================ 
  
-**本篇文章的部分/​全部内容还没有进行翻译。** +- `b:read([formats...]) string...` ​  
- +  先进读取器,可支持多种格式首先,如果不指定`format`调用,即列表留空它会读取下等价于`read("*l")`。  ​ 
-本页描述了如何通过 Lua 访问[[:​component:​zh|组件 API]]。简单来讲:组件是一些块或物品提供的 API以供其以在与之相连的计算机内的 Lua 程序上运行 +每个`format`值都先从IO流中读出次性以多形式返回请注意格式字符串都有 \前缀而且只有字符串的第一个字符有意义,其余字符将会被忽略。   
- +  下是支持的格式:   
-地址 +  ​* ​一个数字值如`10  
---------- +  ​从IO流读取**n**个字节(以二进制模式)或字符(以文本模式),结果将以字符串形式返回。参见[[api:​non-standard-lua-libs#​input_and_output_facilities|io.open]]以获取关如何以不同模式打开文件更多信息。  ​ 
-所有的组件都有地址。地址是一个 UUID,即唯一标识符。大多情况下都可以简写这些地址。例如为了一个缩写的地址获得完整的地址,你可以使用 `component.get`。要获得任何个块的详细地址你可以手持[[item:​analyzer:​zh|分析仪]]按住Shitf键右键该方块。 +  ​示例:`local ​chars b:read(10)`  ​ 
- +\\ 
-要获得所有连接到你的计算机的组件的列表,你可以这样做: +  ​`*n`或者`*number`  ​ 
-```lua +  ​从IO流中读取下一可被解释为数字的字节。请注意读取到的数字会受到打开模式为二进制还是文本的影响。参见[[api:non-standard-lua-libs#​input_and_output_facilities|io.open]]以获取有关何以不模式打开文件的更多信息。   
-Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio +  示例:`local number = b:​read("​*n"​)  
-lua> local component = require("component") +\\ 
-lua> for k,v in component.list() do print(k, v) end +  ​* ​`*l`或者`*line  
-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ​   keyboard +  从IO流中读取下截掉换行标记(可能是 \n,​、\r或\r\n)。 ​  
-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ​   screen +  示例:`local line = b:read("*l"​)` ​  
-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ​   gpu +\\ 
-... +  ​* ​`*L`或者`*Line`  ​ 
-``+  ​从IO流中读取下一行,类似`*line`,但是在结果中保留换行标记。  ​ 
- +  ​示例:`local whole_line ​b:read("*L"​)` ​  
-注意,物品组件永远保持它的地址即使它被从计算机上移除。因此当我们移除一个硬盘再装去后,它地址跟移除前一样块组件则*不是*这样它们在被破坏并重新放置后总是会得到一个新的地址。 +\\ 
- +  * `*a`或者`*all  
-你可以选择像这样过滤组件: +  ​从IO流中读取所有剩余内容,直到遇到nil。在此读取格式后再指定其他格式没有意义。 ​  
- +  示例:`local ​the_whole_file ​b:read("*a")`\\ 
-这段代码将每一个名称包含 "​adar"​ 的组件,如 ​computronics radar 或 warpDrive radar,添加到表 radars 内 +  \\ 
-```lua +`b:getTimeout() number  
-local component = require("​component"​) +  ​返回当前带缓冲流设定的超时时间(单位为秒)。默认超时时间为`math.huge`。参阅`setTimeout`以获取有关于带缓冲流超时时间影响的更多信息。  ​ 
-local radars = {} +\\
-for address, name in component.list("​adar",​ false) do +
-  ​table.insert(radars,​ component.proxy(address)) +
-end +
-``` +
- +
-这段代码只会讲所连接的 warpDrive radars 添加到表 radars 内 +
-```lua +
-local component ​require("​component"​+
-local radars = {} +
-for address, name in component.list("​warpdriveRadar",​ true) do +
-  ​table.insert(radars,​ component.proxy(address)) +
-end +
-``` +
- +
-件 +
------------------- +
-[[api:component:​zh|组件 API]] 会为每种类型的组件都选一个作为“主”组件进行跟踪。果有多个相类型的组,哪一个会被选为主组件是随机的。你可以通过 `component.xxx` 来访问一个指定类型的主组件,其中 `xxx` 是类型名。比如 +
-```lua +
-lua> =component.gpu.address +
-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +
-``+
- +
-或者你也可以使用 ​`component.getPrimary("​xxx"​)`。切记,如果没有指定类型的组件,这么做会引发个错误所以你可能会希望先用 ​`component.isAvailable("xxx"​)` ​进行检查。因为抛出一个错误通常比以后得到一个 nil 更清晰。 +
- +
-这个系统主要是为了让操作系统能够选择最初输出到的屏幕。 +
- +
-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: +
-```lua +
-local component ​require("component") +
-component.invoke(modemAddress,​ "​broadcast",​ port, data) +
-``+
- +
-The preferred way will usually be to get 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: +
-```lua +
-local component ​require("component") +
-local proxy = component.proxy(address) +
- +
--- The call above using the proxy: +
-proxy.broadcast(port, data) +
- +
--- The common proxy fields: +
-print(proxy.address) -- address passed to component.proxy above +
-print(proxy.type) -- "​modem"​ +
-``+
- +
-Note that each primary component you access via `component.getPrimaryor `component.xxxis in fact a proxy. +
- +
-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. +
- +
-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. +
- +
-目录 +
------------ +
-{{page>​component:​contents:​zh&​noheader&​noeditbutton&​nouser&​nofooter}}+