BIOS

From ComputerCraft
Jump to: navigation, search
FunctionFunctionprint
Prints any number of arguments (space separated) at the current cursor position in the current term. All the arguments will automatically be converted to a string using the tostring function, and concatenated, separated by spaces. This function automatically moves to the next line after writing. As well as this, it performs text wrapping, meaning if the string is too big for the term, it will continue writing on the next line. print returns the number of lines it printed.
Syntax print(
  • toPrint... : any
)
Returns number linesPrinted
Part of CC:Tweaked (source)
API BIOS globals
ExampleExamplePrint a basic string
Prints "Hello, world!" to the current term.
Code
print("Hello, world!")
Output
Hello, world!
ExampleExamplePrint multiple strings
Prints "foo bar" to the current term.
Code
print("foo", "bar")
Output
foo bar
ExampleExamplePrint multiple types
Prints strings and numbers to the current term in a single print call.
Code
local cats = 5
local dogs = 3
print("Cats:", cats, "Dogs:", dogs)
Output
Cats: 5 Dogs: 3


FunctionFunctionprintError
Identical to print, however the arguments are printed in red, like an error.
Syntax printError(
  • toPrint... : any
)
Returns number linesPrinted
Part of CC:Tweaked (source)
API BIOS globals
ExampleExamplePrint a basic string
Prints "Hello, world!" to the current term in red.
Code
printError("Hello, world!")
Output
Hello, world!
ExampleExamplePrint multiple strings
Prints "foo bar" to the current term in red.
Code
printError("foo", "bar")
Output
foo bar
ExampleExamplePrint multiple types
Prints strings and numbers to the current term in red, in a single print call.
Code
local cats = 5
local dogs = 3
printError("Cats:", cats, "Dogs:", dogs)
Output
Cats: 5 Dogs: 3


FunctionFunctionread
Reads a string of text at the current cursor position in the current term. typingCharacter will automatically be converted to a string using the tostring function. This function moves to the next line after the user presses enter. Optionally, typingCharacter can be specified so that the user's input is hidden and typingCharacter is written in each typed character's place. read returns the string the user entered. Using the history parameter, it is possible to provide a history of previous commands entered (as an array of strings), so that the user can use the up and down arrow keys to browse through them.
Syntax read()
Returns string textEntered
Part of CC:Tweaked (source)
API BIOS globals
ExampleExampleRead a string of text
Reads a string of user entered text (`Hello World!`) in the current term, assigns it to a variable and then prints it.
Code
local userInput = read()
print(userInput)
Output
Hello World!
ExampleExampleRead a hidden string of text
Reads a string of user entered text (`Hello World!`) in the current term whilst keeping what the user is writing hidden (`************`), assigns it to a variable and then prints it.
Code
local userInput = read("*")
print(userInput)
Output
Hello World!


FunctionFunctionsleep
Pauses execution of the program for time seconds. As it waits for a fixed amount of ticks, time will automatically be rounded up to the nearest multiple of 0.05 seconds. If you are using coroutines or the parallel API, it will only pause execution of the current thread, not the whole program.

Note Note: Because sleep internally uses timers, it is a function that yields. This means that you can use it to prevent "Too long without yielding" errors, however, as the minimum sleep time is 0.05 seconds, it will slow your program down.

Warning Warning: The sleep function internally queues and waits for a timer event, however it does not listen for any other events. This means that any event that occurs while sleeping will be entirely discarded. If you need to recieve events while sleeping, consider using timers.
Syntax sleep()
Returns nil
Part of CC:Tweaked (source)
API BIOS globals
ExampleExampleSleep for a few seconds
Prints "Hello", waits 2 seconds, and then prints "world!".
Code
print("Hello")
sleep(2)
print("world!")
Output
Hello
world!
ExampleExampleSleep for the minimum amount of time
Writes 100 random numbers, one every tick.
Code
for i = 1, 100 do -- Run this 100 times
	local x, y = term.getCursorPos() 
	term.setCursorPos(1, y) -- Go back to the start of the line
	term.clearLine()
	write(math.random())
	sleep(0.05)
end


FunctionFunctionwrite
Writes text at the current cursor position in the current term. Unlike print, this function does not automatically move to the next line after writing, however it does perform text wrapping, meaning if the string is too big for the term, it will continue writing on the next line. write returns the number of extra lines it printed (i.e. if it did not need to wrap to the next line, it will return 0).
Syntax write()
Returns number linesPrinted
Part of CC:Tweaked (source)
API BIOS globals
ExampleExampleWrite to the term
Writes "Hello, world!" to the current term.
Code
write("Hello, world!")
Output
Hello, world!
ExampleExampleWrite multiple strings the term
Writes "foobar" to the current term. Note how there is no space between writes.
Code
write("foo")
write("boo")
Output
foobar
ExampleExampleWrite a very long string
Writes "foo" to the current term 50 times. Note how it automatically wraps to the next line where necessary.
Code
write(("foo "):rep(50))
Output Demo write multiple.png