OS (API)

From ComputerCraft Wiki
Revision as of 20:12, 21 April 2013 by Hawk777 (Talk | contribs) (Grammar)

Jump to: navigation, search

The Operating System API allows for interfacing with the Lua based Operating System itself.

Grid disk.png   OS (API)

Method NameDescription
os.version() Returns the version of the OS the computer is running.
os.getComputerID() Returns the unique ID of this computer. os.computerID() also behaves exactly the same as os.getComputerID().
os.getComputerLabel() Returns the label of this computer. os.computerLabel() also behaves exactly the same as os.getComputerLabel().
os.setComputerLabel( label ) Set the label of this computer.
os.run( environment, programpath, arguments ) An advanced way of starting programs. A started program will have a given environment table which determines what functions it has available, as well as any variables it will be able to access by default. You may prefer to use the Shell (API) unless you need to do something special.
os.loadAPI( name ) Loads a Lua script as an API in its own namespace (see example). It will be available to all programs that run on the terminal.
os.unloadAPI( name ) Unloads a previously loaded API.
os.pullEvent( target-event ) Blocks until the computer receives an event, or if target-event is specified, will block until an instance of target-event occurs. os.pullEvent( target-event ) returns the event and any parameters the event may have. If a target-event is specified, the computer will not break for any other events (except termination).
os.pullEventRaw() Advanced version of pullEvent(). os.pullEventRaw() will block until an event occurs, and then returns the event (any any parameters the event may have). Unlike os.pullEvent( target-event ), this function will not break for system events, and can be used to handle termination events.
os.queueEvent( event, param1, param2, ... ) Adds an event to the event queue with the name event and the given parameters
os.clock() Returns CPU time.
os.startTimer( timeout ) Queues an event to be triggered after a number of seconds (timeout). The ID of the timer is returned from this function to differentiate multiple timers. Timers are one-shot; once they have fired an event you will need to start another one if you need a recurring timer.
os.sleep( timeout ) Makes the system wait a number of seconds before continuing in the program. os.sleep( timeout ) may also be used as simply "sleep( timeout )".
os.time() Returns the current Minecraft world time.
os.day() Returns the current Minecraft day.
os.setAlarm( time ) Queues an event to be triggered at the specified Minecraft world time.
os.shutdown() Turns off the computer.
os.reboot() Reboots the computer.

APIs

APIs are Lua files which are loaded into the OS itself, and expose functions which other programs may use. The stock APIs that ship with ComputerCraft are loaded in this way, and may be replaced by a computer’s user or programs.

The following is an example of a valid implementation of an API, and its usage after being registered:

 -- "apiTest" file
 function foo(bar)
   print(bar)
 end
 
 -- "program" file
 os.loadAPI("apiTest")
 apiTest.foo("this is a test")

os.pullEvent()

os.pullEvent() causes the current program to pause, retrieving the next event from the computer's queue of events. If there is no event to read, then the program will stall until such an event becomes available. Note that if a program has not attempted to pull an event in the past ten seconds, it will be forcefully terminated to prevent CPU waste in SMP environments.

os.pullEvent() returns the name of the event that was read, as well as up to five parameters: local event, p1, p2, p3, p4, p5 = os.pullEvent()

os.pullEvent() is usually run inside a 'while true do' loop.

An advanced version of this method os.pullEventRaw bypasses the normal handling of events from the OS. You may use this to act on the "Terminate" event (triggered when holding Ctrl-T) for custom termination logic.