**This is an old revision of the document!**

Computer(电脑) API

此API主要提供了关于正在运行Lua state的电脑的信息,例如其地址和运行时间。API中还包含了用于用户管理的函数。此API中的函数可以从属于os API,但是为了保持“整洁”,这些函数归类为单独的API。

  • computer.address(): string
    此电脑的组件地址
  • computer.tmpAddress(): string
    此电脑的临时文件系统组件地址(如果有),用于在启动时挂载此文件系统。
  • computer.freeMemory(): number
    当前未使用的内存大小,单位为字节。如果此数值接近0那么你的电脑可能会很快因为内存不足而崩溃。请注意,为了运行OpenOS,高度建议用户安装1条以上1.5级内存条。系统能用单条1级内存启动,但很快就容易内存不足。
  • computer.totalMemory(): number
    此电脑安装的内存总量,单位为字节。
  • computer.energy(): number
    电脑所在的网络中当前可用的能量数量。对机器人而言是机器人自己的能量/燃料量。
  • computer.maxEnergy(): number
    电脑所在的网络中能够存储的最大能量数量。对机器人而言是机器人的内置能量缓存大小(可以在机器人的GUI中查看)。
  • computer.uptime(): number
    The time in real world seconds this computer has been running, measured based on the world time that passed since it was started - meaning this will not increase while the game is paused, for example.
  • computer.shutdown([reboot: boolean])
    关闭电脑。如果reboottrue也可以重启电脑,即先关机再自动启动。此函数不会有返回值。 下面的样例将会在电脑运行至少300秒(5分钟)后重启电脑。
snippet.lua
local computer = require("computer")
if computer.uptime() >= 300 then
    computer.shutdown(true)
end
  • computer.getBootAddress():string
    获取首先尝试启动的文件系统组件的地址。OC 1.3新加入
  • computer.setBootAddress([address:string])
    设定首先尝试启动的文件系统组件地址。传参nil/不传参调用以清除。OC 1.3新加入
  • computer.runlevel(): string|number

    返回电脑当前所在的运行状态(英文维基百科)。OpenOS的运行状态有:

    • S: 单用户模式,尚未有组件或文件系统初始化
    • 1: 单用户模式,文件系统与组件已初始化——OpenOS完成了启动
  • computer.users(): string, ...
    A list of all users registered on this computer, as a tuple. To iterate the result as a list, use table.pack on it, first. Please see the user rights documentation.
  • computer.addUser(name: string): boolean or nil, string
    Registers a new user with this computer. Returns true if the user was successfully added. Returns nil and an error message otherwise.
    The user must be currently in the game. The user will gain full access rights on the computer. In the shell, useradd USER is a command line option to invoke this method.
  • computer.removeUser(name: string): boolean
    Unregisters a user from this computer. Returns true if the user was removed, false if they weren't registered in the first place.
    The user will lose all access to this computer. When the last user is removed from the user list, the computer becomes accessible to all players. userdel USER is a command line option to invoke this method.
  • computer.pushSignal(name: string[, ...])
    Pushes a new signal into the queue. Signals are processed in a FIFO order. The signal has to at least have a name. Arguments to pass along with it are optional. Note that the types supported as signal parameters are limited to the basic types nil, boolean, number, string, and tables. Yes tables are supported (keep reading). Threads and functions are not supported.
    Note that only tables of the supported types are supported. That is, tables must compose types supported, such as other strings and numbers, or even sub tables. But not of functions or threads.
  • computer.pullSignal([timeout: number]): name, ...
    Tries to pull a signal from the queue, waiting up to the specified amount of time before failing and returning nil. If no timeout is specified waits forever.
    The first returned result is the signal name, following results correspond to what was pushed in pushSignal, for example. These vary based on the event type. Generally it is more convenient to use event.pull from the event library. The return value is the very same, but the event library provides some more options.
  • computer.beep([frequency:string or number[, duration: number])

    if frequency is a number it value must be between 20 and 2000.

    Causes the computer to produce a beep sound at frequency Hz for duration seconds. This method is overloaded taking a single string parameter as a pattern of dots . and dashes - for short and long beeps respectively.

  • computer.getDeviceInfo(): table

    Returns a table of information about installed devices in the computer.

目录