API/computer: Difference between revisions

From OpenComputers: Rebooted
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 1: Line 1:
= Computer API =
= Computer API =


This API mainly provides information about the computer a Lua state is running on, such as its address and uptime. It also contains functions for user management. This could belong to the `os` table, but in order to keep that "clean" it's in its own API.
This API mainly provides information about the computer a Lua state is running on, such as its address and uptime. It also contains functions for user management. This could belong to the <code>os</code> table, but in order to keep that "clean" it's in its own API.


* `computer.address(): string`
* <code>computer.address(): string</code>


<pre>The [[:component:component_access|component address]] of this computer.
<pre>The [[:component:component_access|component address]] of this computer.
</pre>
</pre>
* `computer.tmpAddress(): string`
* <code>computer.tmpAddress(): string</code>


<pre>The component address of the computer's temporary file system (if any), used for mounting it on startup.
<pre>The component address of the computer's temporary file system (if any), used for mounting it on startup.
</pre>
</pre>
* `computer.freeMemory(): number`
* <code>computer.freeMemory(): number</code>


<pre>The amount of memory currently unused, in bytes. If this gets close to zero your computer will probably soon crash with an out of memory error. Note that for OpenOS, it is highly recommended to at least have 1x tier 1.5 RAM stick or more. The os will boot on a single tier 1 ram stick, but quickly and easily run out of memory.
<pre>The amount of memory currently unused, in bytes. If this gets close to zero your computer will probably soon crash with an out of memory error. Note that for OpenOS, it is highly recommended to at least have 1x tier 1.5 RAM stick or more. The os will boot on a single tier 1 ram stick, but quickly and easily run out of memory.
</pre>
</pre>
* `computer.totalMemory(): number`
* <code>computer.totalMemory(): number</code>


<pre>The total amount of memory installed in this computer, in bytes.
<pre>The total amount of memory installed in this computer, in bytes.
</pre>
</pre>
* `computer.energy(): number`
* <code>computer.energy(): number</code>


<pre>The amount of energy currently available in the network the computer is in. For a robot this is the robot's own energy / fuel level.
<pre>The amount of energy currently available in the network the computer is in. For a robot this is the robot's own energy / fuel level.
</pre>
</pre>
* `computer.maxEnergy(): number`
* <code>computer.maxEnergy(): number</code>


<pre>The maximum amount of energy that can be stored in the network the computer is in. For a robot this is the size of the robot's internal buffer (what you see in the robot's GUI).
<pre>The maximum amount of energy that can be stored in the network the computer is in. For a robot this is the size of the robot's internal buffer (what you see in the robot's GUI).
</pre>
</pre>
* `computer.uptime(): number`
* <code>computer.uptime(): number</code>


<pre>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.
<pre>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.
</pre>
</pre>
* `computer.shutdown([reboot: boolean])`
* <code>computer.shutdown([reboot: boolean])</code>


<pre>Shuts down the computer. Optionally reboots the computer, if `reboot` is true, i.e. shuts down, then starts it again automatically. This function never returns.
<pre>Shuts down the computer. Optionally reboots the computer, if reboot is true, i.e. shuts down, then starts it again automatically. This function never returns.
This example will reboot the computer if it has been running for at least 300 seconds(5 minutes)
This example will reboot the computer if it has been running for at least 300 seconds(5 minutes)
</pre>
</pre>
```lua local computer = require("computer") if computer.uptime() >= 300 then
<syntaxhighlight lang="lua">
local computer = require("computer") if computer.uptime() >= 300 then


<pre>  computer.shutdown(true)
  computer.shutdown(true)
</pre>
 
end ```
end
</syntaxhighlight>


* `computer.getBootAddress():string`
* <code>computer.getBootAddress():string</code>


<pre>Get the address of the filesystem component from which to try to boot first. ''New since OC 1.3''.
<pre>Get the address of the filesystem component from which to try to boot first. ''New since OC 1.3''.
</pre>
</pre>
* `computer.setBootAddress([address:string])`
* <code>computer.setBootAddress([address:string])</code>


<pre>Set the address of the filesystem component from which to try to boot first. Call with nil / no arguments to clear. ''New since OC 1.3''.
<pre>Set the address of the filesystem component from which to try to boot first. Call with nil / no arguments to clear. ''New since OC 1.3''.
</pre>
</pre>
* `computer.runlevel(): string|number`
* <code>computer.runlevel(): string|number</code>


<pre>Returns the current [[https://en.wikipedia.org/wiki/Runlevel|runlevel]] the computer is in. Current Runlevels in OpenOS are:
<pre>Returns the current [[https://en.wikipedia.org/wiki/Runlevel|runlevel]] the computer is in. Current Runlevels in OpenOS are:
* `S`: Single-User mode, no components or filesystems initialized yet
* S: Single-User mode, no components or filesystems initialized yet
* `1`: Single-User mode, filesystems and components initialized - OpenOS finished booting
* 1: Single-User mode, filesystems and components initialized - OpenOS finished booting
</pre>
</pre>
* `computer.users(): string, ...`
* <code>computer.users(): string, ...</code>


<pre>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.
<pre>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 [[:computer_users|the user rights documentation]].
Please see [[:computer_users|the user rights documentation]].
</pre>
</pre>
* `computer.addUser(name: string): boolean or nil, string`
* <code>computer.addUser(name: string): boolean or nil, string</code>


<pre>Registers a new user with this computer. Returns `true` if the user was successfully added. Returns `nil` and an error message otherwise.
<pre>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.
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.
</pre>
</pre>
* `computer.removeUser(name: string): boolean`
* <code>computer.removeUser(name: string): boolean</code>


<pre>Unregisters a user from this computer. Returns `true` if the user was removed, `false` if they weren't registered in the first place.
<pre>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.
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.
</pre>
</pre>
* `computer.pushSignal(name: string[, ...])`
* <code>computer.pushSignal(name: string[, ...])</code>


<pre>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.
<pre>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.
Line 77: Line 79:
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.
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.
</pre>
</pre>
* `computer.pullSignal([timeout: number]): name, ...`
* <code>computer.pullSignal([timeout: number]): name, ...</code>


<pre>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.
<pre>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.
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 [[API/event|event]] library. The return value is the very same, but the `event` library provides some more options.
Generally it is more convenient to use event.pull from the [[API/event|event]] library. The return value is the very same, but the event library provides some more options.
</pre>
</pre>
* `computer.beep([frequency:string or number[, duration: number])`
* <code>computer.beep([frequency:string or number[, duration: number])</code>


<pre>if `frequency` is a number it value must be between 20 and 2000.
<pre>if frequency is a number it value must be between 20 and 2000.
</pre>
</pre>
<pre>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.
<pre>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.
</pre>
</pre>
* `computer.getDeviceInfo(): table`
* <code>computer.getDeviceInfo(): table</code>


<pre>Returns a table of information about installed devices in the computer.
<pre>Returns a table of information about installed devices in the computer.
Line 96: Line 98:


{{:API/contents}}
{{:API/contents}}

Latest revision as of 20:26, 24 August 2026

Computer API

This API mainly provides information about the computer a Lua state is running on, such as its address and uptime. It also contains functions for user management. This could belong to the os table, but in order to keep that "clean" it's in its own API.

  • computer.address(): string
The [[:component:component_access|component address]] of this computer.
  • computer.tmpAddress(): string
The component address of the computer's temporary file system (if any), used for mounting it on startup.
  • computer.freeMemory(): number
The amount of memory currently unused, in bytes. If this gets close to zero your computer will probably soon crash with an out of memory error. Note that for OpenOS, it is highly recommended to at least have 1x tier 1.5 RAM stick or more. The os will boot on a single tier 1 ram stick, but quickly and easily run out of memory.
  • computer.totalMemory(): number
The total amount of memory installed in this computer, in bytes.
  • computer.energy(): number
The amount of energy currently available in the network the computer is in. For a robot this is the robot's own energy / fuel level.
  • computer.maxEnergy(): number
The maximum amount of energy that can be stored in the network the computer is in. For a robot this is the size of the robot's internal buffer (what you see in the robot's 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])
Shuts down the computer. Optionally reboots the computer, if reboot is true, i.e. shuts down, then starts it again automatically. This function never returns.
This example will reboot the computer if it has been running for at least 300 seconds(5 minutes)
local computer = require("computer") if computer.uptime() >= 300 then

  computer.shutdown(true)

end
  • computer.getBootAddress():string
Get the address of the filesystem component from which to try to boot first. ''New since OC 1.3''.
  • computer.setBootAddress([address:string])
Set the address of the filesystem component from which to try to boot first. Call with nil / no arguments to clear. ''New since OC 1.3''.
  • computer.runlevel(): string|number
Returns the current [[https://en.wikipedia.org/wiki/Runlevel|runlevel]] the computer is in. Current Runlevels in OpenOS are:
* S: Single-User mode, no components or filesystems initialized yet
* 1: Single-User mode, filesystems and components initialized - OpenOS finished booting
  • 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 [[:computer_users|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 [[API/event|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.

Contents

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